Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.52 KB | None | 0 0
  1.  
  2.  
  3.  
  4. import javax.swing.text.*;
  5. import javax.swing.*;
  6. import java.awt.event.*;
  7. import java.awt.*;
  8. import java.text.*;
  9. import java.io.*;
  10. import java.util.*;
  11.  
  12. public class RForm extends JFrame
  13. {
  14. ArrayList persons;
  15.  
  16. public static void main(String [] args)
  17. {
  18. new RForm();
  19. }
  20.  
  21. JTextField name, fname, add, pro;
  22. JFormattedTextField cnic, age;
  23. JButton saveButton, exitButton;
  24.  
  25.  
  26.  
  27. public RForm()
  28. {
  29.  
  30. persons = new ArrayList();
  31.  
  32. this.setTitle("Voter Registration Form");
  33. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. JPanel panel1 = new JPanel();
  35.  
  36. ButtonListener b1 = new ButtonListener();
  37. ButtonListener b2 = new ButtonListener();
  38.  
  39. panel1.setLayout(new GridBagLayout());
  40. addItem(panel1, new JLabel("Voter Registration Form"),
  41. 0, 0, 2, 1, GridBagConstraints.NORTH);
  42. addItem(panel1, new JLabel("CNIC"),
  43. 0, 1, 1, 1, GridBagConstraints.EAST);
  44. addItem(panel1, new JLabel("Name"),
  45. 0, 2, 1, 1, GridBagConstraints.EAST);
  46. addItem(panel1, new JLabel("Father/Husband Name"),
  47. 0, 3, 1, 1, GridBagConstraints.EAST);
  48. addItem(panel1, new JLabel("Age"),
  49. 0, 4, 1, 1, GridBagConstraints.EAST);
  50. addItem(panel1, new JLabel("Address"),
  51. 0, 5, 1, 1, GridBagConstraints.EAST);
  52. addItem(panel1, new JLabel("Province"),
  53. 0, 6, 1, 1, GridBagConstraints.EAST);
  54.  
  55. MaskFormatter cnicMask = null;
  56.  
  57. try {
  58.  
  59. cnicMask = new MaskFormatter("#####-#######-#");
  60. }catch (ParseException e){
  61. e.printStackTrace();
  62. }
  63.  
  64. MaskFormatter ageMask = null;
  65.  
  66. try {
  67.  
  68. ageMask = new MaskFormatter("##");
  69. }catch (ParseException e){
  70. e.printStackTrace();
  71. }
  72.  
  73. cnic = new JFormattedTextField(cnicMask);
  74. cnic.setPreferredSize(new Dimension(225,20));
  75. name = new JTextField(20);
  76. fname = new JTextField(20);
  77. age = new JFormattedTextField(ageMask);
  78. age.setPreferredSize(new Dimension(225,20));
  79. add = new JTextField(20);
  80. pro = new JTextField(20);
  81.  
  82. addItem(panel1, cnic, 1, 1, 1, 1,
  83. GridBagConstraints.WEST);
  84. addItem(panel1, name, 1, 2, 2, 1,
  85. GridBagConstraints.WEST);
  86. addItem(panel1, fname, 1, 3, 3, 1,
  87. GridBagConstraints.WEST);
  88. addItem(panel1, age, 1, 4, 4, 1,
  89. GridBagConstraints.WEST);
  90. addItem(panel1, add, 1, 5, 5, 1,
  91. GridBagConstraints.WEST);
  92. addItem(panel1, pro, 1, 6, 6, 1,
  93. GridBagConstraints.WEST);
  94.  
  95.  
  96. Box sizeBox = Box.createVerticalBox();
  97.  
  98.  
  99. Box buttonBox = Box.createHorizontalBox();
  100. saveButton = new JButton("Save");
  101. exitButton = new JButton("Exit");
  102. buttonBox.add(saveButton);
  103. buttonBox.add(Box.createHorizontalStrut(20));
  104. buttonBox.add(exitButton);
  105. addItem(panel1, buttonBox, 1, 7, 7, 1,
  106. GridBagConstraints.WEST);
  107.  
  108. saveButton.addActionListener(b1);
  109. exitButton.addActionListener(b2);
  110.  
  111.  
  112. this.add(panel1);
  113. this.pack();
  114. this.setVisible(true);
  115. }
  116.  
  117.  
  118. private void addItem(JPanel p, JComponent c,
  119. int x, int y, int width, int height, int align)
  120. {
  121. GridBagConstraints gc = new GridBagConstraints();
  122. gc.gridx = x;
  123. gc.gridy = y;
  124. gc.gridwidth = width;
  125. gc.gridheight = height;
  126. gc.weightx = 100.0;
  127. gc.weighty = 100.0;
  128. gc.insets = new Insets(5, 5, 5, 5);
  129. gc.anchor = align;
  130. gc.fill = GridBagConstraints.NONE;
  131. p.add(c, gc);
  132. }
  133.  
  134.  
  135.  
  136. public void addPerson(){
  137.  
  138. String cnic1 = (String)cnic.getValue();
  139. String name1 = name.getText();
  140. String fname1 = fname.getText();
  141. String age1 = (String)age.getValue();
  142. String add1 = add.getText();
  143. String pro1 = pro.getText();
  144.  
  145. PersonInfo p = new PersonInfo(cnic1, name1, fname1, age1, add1, pro1);
  146. persons.add(p);
  147.  
  148. }
  149.  
  150. public void ClearField(){
  151.  
  152. cnic.setValue("");
  153. name.setText("");
  154. fname.setText("");
  155. age.setValue("");
  156. add.setText("");
  157. pro.setText("");
  158.  
  159.  
  160.  
  161. }
  162.  
  163. public void saveInfo (){
  164.  
  165. try{
  166.  
  167. FileWriter fr = new FileWriter ("C:\\Documents and Settings\\Administrator\\My Documents\\RForm\\input.txt");
  168. PrintWriter pw = new PrintWriter (fr);
  169.  
  170. String s = "";
  171.  
  172. for (int i = 0; i<persons.size(); i++){
  173.  
  174. PersonInfo p = (PersonInfo)persons.get(i);
  175.  
  176. s = p.cnic + "," + p.name + "," + p.fname + "," + p.age + "," + p.add + "," + p.pro;
  177. pw.println(s);
  178. }
  179. pw.close();
  180. fr.close();
  181.  
  182.  
  183.  
  184. }catch(IOException e){
  185.  
  186. JOptionPane.showMessageDialog(RForm.this, "Unable to write the file !", "Error",JOptionPane.INFORMATION_MESSAGE);
  187.  
  188. }
  189. }
  190.  
  191. private class ButtonListener implements ActionListener
  192. {
  193. public void actionPerformed (ActionEvent e)
  194. {
  195. while(true){
  196.  
  197. if (e.getSource() == exitButton)
  198. {
  199. System.exit(0);
  200. }
  201.  
  202. if (e.getSource() == saveButton)
  203.  
  204. {
  205. String cnic1 = (String)cnic.getValue();
  206. String name1 = name.getText();
  207. String fname1 = fname.getText();
  208. String age1 = (String)age.getValue();
  209. String add1 = add.getText();
  210. String pro1 = pro.getText();
  211.  
  212. if (cnic1 != null)
  213. {
  214.  
  215. if (name1.length()==0)
  216. {
  217. JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Name!", "Error",JOptionPane.INFORMATION_MESSAGE);
  218. return;
  219. }else if (fname1.length() == 0)
  220. {
  221. JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Father/Husband Name!", "Error",JOptionPane.INFORMATION_MESSAGE);
  222. return;
  223. }
  224.  
  225. }else{
  226.  
  227. JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in CNIC!", "Error",JOptionPane.INFORMATION_MESSAGE);
  228. return;
  229. }
  230.  
  231. if(age1 != null)
  232. {
  233. if (add1.length() == 0)
  234. {
  235. JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Address!", "Error",JOptionPane.INFORMATION_MESSAGE);
  236. return;
  237. }else if (pro1.length() == 0)
  238. {
  239. JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Province!", "Error",JOptionPane.INFORMATION_MESSAGE);
  240. return;
  241. }else
  242.  
  243. addPerson();
  244. saveInfo();
  245. ClearField();
  246.  
  247. }else {
  248.  
  249. JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Age!", "Error",JOptionPane.INFORMATION_MESSAGE);
  250.  
  251. }
  252.  
  253. }
  254.  
  255. }
  256. }
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement