Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.text.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.text.*;
- import java.io.*;
- import java.util.*;
- public class RForm extends JFrame
- {
- ArrayList persons;
- public static void main(String [] args)
- {
- new RForm();
- }
- JTextField name, fname, add, pro;
- JFormattedTextField cnic, age;
- JButton saveButton, exitButton;
- public RForm()
- {
- persons = new ArrayList();
- this.setTitle("Voter Registration Form");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JPanel panel1 = new JPanel();
- ButtonListener b1 = new ButtonListener();
- ButtonListener b2 = new ButtonListener();
- panel1.setLayout(new GridBagLayout());
- addItem(panel1, new JLabel("Voter Registration Form"),
- 0, 0, 2, 1, GridBagConstraints.NORTH);
- addItem(panel1, new JLabel("CNIC"),
- 0, 1, 1, 1, GridBagConstraints.EAST);
- addItem(panel1, new JLabel("Name"),
- 0, 2, 1, 1, GridBagConstraints.EAST);
- addItem(panel1, new JLabel("Father/Husband Name"),
- 0, 3, 1, 1, GridBagConstraints.EAST);
- addItem(panel1, new JLabel("Age"),
- 0, 4, 1, 1, GridBagConstraints.EAST);
- addItem(panel1, new JLabel("Address"),
- 0, 5, 1, 1, GridBagConstraints.EAST);
- addItem(panel1, new JLabel("Province"),
- 0, 6, 1, 1, GridBagConstraints.EAST);
- MaskFormatter cnicMask = null;
- try {
- cnicMask = new MaskFormatter("#####-#######-#");
- }catch (ParseException e){
- e.printStackTrace();
- }
- MaskFormatter ageMask = null;
- try {
- ageMask = new MaskFormatter("##");
- }catch (ParseException e){
- e.printStackTrace();
- }
- cnic = new JFormattedTextField(cnicMask);
- cnic.setPreferredSize(new Dimension(225,20));
- name = new JTextField(20);
- fname = new JTextField(20);
- age = new JFormattedTextField(ageMask);
- age.setPreferredSize(new Dimension(225,20));
- add = new JTextField(20);
- pro = new JTextField(20);
- addItem(panel1, cnic, 1, 1, 1, 1,
- GridBagConstraints.WEST);
- addItem(panel1, name, 1, 2, 2, 1,
- GridBagConstraints.WEST);
- addItem(panel1, fname, 1, 3, 3, 1,
- GridBagConstraints.WEST);
- addItem(panel1, age, 1, 4, 4, 1,
- GridBagConstraints.WEST);
- addItem(panel1, add, 1, 5, 5, 1,
- GridBagConstraints.WEST);
- addItem(panel1, pro, 1, 6, 6, 1,
- GridBagConstraints.WEST);
- Box sizeBox = Box.createVerticalBox();
- Box buttonBox = Box.createHorizontalBox();
- saveButton = new JButton("Save");
- exitButton = new JButton("Exit");
- buttonBox.add(saveButton);
- buttonBox.add(Box.createHorizontalStrut(20));
- buttonBox.add(exitButton);
- addItem(panel1, buttonBox, 1, 7, 7, 1,
- GridBagConstraints.WEST);
- saveButton.addActionListener(b1);
- exitButton.addActionListener(b2);
- this.add(panel1);
- this.pack();
- this.setVisible(true);
- }
- private void addItem(JPanel p, JComponent c,
- int x, int y, int width, int height, int align)
- {
- GridBagConstraints gc = new GridBagConstraints();
- gc.gridx = x;
- gc.gridy = y;
- gc.gridwidth = width;
- gc.gridheight = height;
- gc.weightx = 100.0;
- gc.weighty = 100.0;
- gc.insets = new Insets(5, 5, 5, 5);
- gc.anchor = align;
- gc.fill = GridBagConstraints.NONE;
- p.add(c, gc);
- }
- public void addPerson(){
- String cnic1 = (String)cnic.getValue();
- String name1 = name.getText();
- String fname1 = fname.getText();
- String age1 = (String)age.getValue();
- String add1 = add.getText();
- String pro1 = pro.getText();
- PersonInfo p = new PersonInfo(cnic1, name1, fname1, age1, add1, pro1);
- persons.add(p);
- }
- public void ClearField(){
- cnic.setValue("");
- name.setText("");
- fname.setText("");
- age.setValue("");
- add.setText("");
- pro.setText("");
- }
- public void saveInfo (){
- try{
- FileWriter fr = new FileWriter ("C:\\Documents and Settings\\Administrator\\My Documents\\RForm\\input.txt");
- PrintWriter pw = new PrintWriter (fr);
- String s = "";
- for (int i = 0; i<persons.size(); i++){
- PersonInfo p = (PersonInfo)persons.get(i);
- s = p.cnic + "," + p.name + "," + p.fname + "," + p.age + "," + p.add + "," + p.pro;
- pw.println(s);
- }
- pw.close();
- fr.close();
- }catch(IOException e){
- JOptionPane.showMessageDialog(RForm.this, "Unable to write the file !", "Error",JOptionPane.INFORMATION_MESSAGE);
- }
- }
- private class ButtonListener implements ActionListener
- {
- public void actionPerformed (ActionEvent e)
- {
- while(true){
- if (e.getSource() == exitButton)
- {
- System.exit(0);
- }
- if (e.getSource() == saveButton)
- {
- String cnic1 = (String)cnic.getValue();
- String name1 = name.getText();
- String fname1 = fname.getText();
- String age1 = (String)age.getValue();
- String add1 = add.getText();
- String pro1 = pro.getText();
- if (cnic1 != null)
- {
- if (name1.length()==0)
- {
- JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Name!", "Error",JOptionPane.INFORMATION_MESSAGE);
- return;
- }else if (fname1.length() == 0)
- {
- JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Father/Husband Name!", "Error",JOptionPane.INFORMATION_MESSAGE);
- return;
- }
- }else{
- JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in CNIC!", "Error",JOptionPane.INFORMATION_MESSAGE);
- return;
- }
- if(age1 != null)
- {
- if (add1.length() == 0)
- {
- JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Address!", "Error",JOptionPane.INFORMATION_MESSAGE);
- return;
- }else if (pro1.length() == 0)
- {
- JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Province!", "Error",JOptionPane.INFORMATION_MESSAGE);
- return;
- }else
- addPerson();
- saveInfo();
- ClearField();
- }else {
- JOptionPane.showMessageDialog(RForm.this, "You didn't enter anyting in Age!", "Error",JOptionPane.INFORMATION_MESSAGE);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement