i have made this little program in GUI, Please help in these following error.
1st time when we save the data second time it comes to 2nd field name not on cnic field, it should ask cnic field first then other ask for other fields.

Please any one guide me ...

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);

                }

                }

            }
        }
    }
}

Recommended Answers

All 6 Replies

1. Please post your code inside code tags.
2. remove while (true) from actionPerformed in ButtonListener.
3. add cnic.requestFocus(); to the end of your ClearField method.

commented: thanx a lot for your guidline +0

thanx a lot kram erd

kram can you please help me more ... when i compile this program i am getting following message in status bar ... how can i remove it ...

Note: C:\Documents and Settings\Administrator\My Documents\RForm\RForm.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Tool completed successfully

I can't be sure what the problem is, but this might fix it:

1. change ArrayList persons; to ArrayList<PersonInfo> persons; 2. change persons = new ArrayList(); to persons = new ArrayList<PersonInfo>();

its done ... thanx a lot :) for your time and guideline ...

You're welcome. Please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.