简体   繁体   中英

Java swing save several text field to a object

Im quite new to java swing but im creating a stock management program, i have now my window registration form were the user can insert, name, adress, username, password, etc, and i have a button below to save all data. I want to e able to press the save button and all the data be saved to a new userObject, my problem is how to associate each textfield data so they can be saved to new object in save button. Can anyone give me some pointers on how to do this?

Thanks!

I tried the code this way:

private void ButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {

   User obj=new User();
   String userName = UserTextFieldName.getText();
   String userAdress = UserTextFieldAdress.getText();
   String userCitizenID = UserTextFieldCitizenID.getInteger();
   obj.setName(userName);
   obj.setAdress(userAdress);
   obj.setCitizenID(userCitizenID);

   usersArrayList.add(obj);

//this is were i would give a save correctly message and dispose the registration window.

Is this a good alternative method?i could not make it working the other way with the ActionListener...

You add an ActonListener to your button.

Something like:

JButton save = new JButton( "Save" );
save.addActionListener( new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    }
        UserObject user = new UserObject();
        user.setName( nameTextField.getText() );
        user.setAddress( addressTextField.getText() );
        ...
    }
});

Read the Swing tutorial for Swing basics.

There are sections on:

  1. How to Use Buttons
  2. How to Write an ActionListener

that each contain working examples to get your started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM