简体   繁体   中英

how to get and pass value from JTextField

I've created class with JTextField and I want to get this value and use in other class.

public class Create{
 JTextField user;

public Create(){
  user =new JTextField(10);
  }
public  String getUser(){
  return user.getText();
  }



public class ReviewWindow{
 JLabel luser1;
 public ReviewWindow(){
  Create create = new Create();
  luser1= new JLabel(create.getUser()); //I want to get this value here.
    }
  }

So usually there is an action that is associated with getting a value from a JTextField. For example a button named "save" that is pressed once the user is finished typing in the JTextfield. So set up a button (an tutorial/explanation can be found here ) add an action listener to that button. Inside that actionlistener function, grab the text from JtextField (JtextField.getText()) and pass it to your other class via a getter/setter function or constructor.

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