-3

i want the window to close as soon as the input has been typed and clicked on add button. also i would like a message to appear notifying the user that the input data has been saved. at the moment this code is linked to a database object where it will store the input.

public class Add extends JFrame
             implements ActionListener {

/** {@link JTextField} where the user name is entered */
JTextField Inputusername = new JTextField(7);

/** {@link JTextField} where the user age is entered */
JTextField age = new JTextField(2);

/** {@link JTextField} where the user ID is entered */
JTextField inputuserid = new JTextField(4);

/** Add Client button */

JButton addnewclient = new JButton("Add Client");
/** male Jradiobutton */
JRadioButton male = new JRadioButton("Male");
/** female Jradiobutton */
JRadioButton female = new JRadioButton("Female");
/** label for the gender selection */
Label genders = new Label("please select gender of client");

/** call the database constructor*/

private Database db; 
public Add(Database db) 
    { this.db = db; 


    //allows the positioning 
    setLayout(new BorderLayout()); 

  //setting the size of the window
    setBounds(100, 100, 500, 200); 

 // the title of the window
    setTitle("add new Client"); 

    // dispose of the window when the close button is clicked
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

 // declared new panel  
    JPanel top = new JPanel(); 
    top.add(new JLabel("Enter username :"));
    top.add(Inputusername);
    top.add(new JLabel("Enter age:"));
    top.add(age);
    top.add(new JLabel("Enter userid:"));
    top.add(inputuserid);
    add("North",top);
    // declared new panel 
    JPanel bottom = new JPanel(); 
 // add the veritable of JButton to the top panel
    bottom.add(addnewclient); 
 // add the bottom panel to the bottom of the screen
    add("South",bottom); 

    JPanel middle = new JPanel();
    ButtonGroup bg = new ButtonGroup();
    bg.add(male);
    bg.add(female);
    middle.add(male);
    middle.add(female);
    add("Center",middle);

 // do not allow user to set the size of the screen
    setResizable(false);
 // make the program  visible
    setVisible(true);
 // listen to the button
   addnewclient.addActionListener(this);   
}  

public void actionPerformed(ActionEvent e) {

    String selection = "female"; 

    if (this.male.isSelected()) 
    {
         selection = "male"; 
    }

    User u = new User(Inputusername.getText(),  selection , age.getText(), inputuserid.getText());
    db.addUser(u); 

}

skaffman
  • 398,947
  • 96
  • 818
  • 769
Sirnur
  • 13
  • 1

2 Answers2

5

Use JOptionPane, you have to look at JOptionPane#showXxxDialog, that could be returned desired workaround

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • thank you very much i got it to work with the JOptionPane, my programe looks more professional. – Sirnur Mar 19 '12 at 21:31
5

You can add some code to your action performed method. Have a look at the API for JFrame for a list of methods, but what you probably want is call this.dispose();.

There are also some existing answers on stack overflow already such as this.

EDIT: Be aware that this will dispose of the class you are using too; so if you still need some of the busy logic, then you want a method that hides the JFrame instead. I'm sure you can find this yourself, but it would be a better idea to separate out GUI from business logic.

Community
  • 1
  • 1
dann.dev
  • 2,406
  • 3
  • 20
  • 32
  • dam.dev thank you very much I looked through the API, this is very important contains all the classes with explanations. thanks alot – Sirnur Mar 19 '12 at 21:34
  • No worries, try to say exactly which part you are having trouble with next time, then people can offer better help sooner – dann.dev Mar 19 '12 at 22:24