2

Someone please help me? How can I print the text I typed in the textfield into another class? Is thing has something to do with get and set text? This is my first class

public class Name extends JFrame {

JFrame frame = new JFrame ();
JPanel panel1 = new JPanel();
JLabel yourname = new JLabel("Name");
JTextField text = new JTextField(30);


public Name(){

    add(yourname);
    add(text);

    setLayout(new FlowLayout());
    setVisible(true);
    setSize(900,600);

}


public static void main(String[] args) {

    Name go = new Name();
   go.setVisible(true);
   go.setDefaultCloseOperation(EXIT_ON_CLOSE);
   go.setLocationRelativeTo(null);
}}

And this is my second class

public class Output extends JFrame {
JFrame frame1 = new JFrame();

Output(){

    add(yourname); 


}

public static void main(String args[]){

    Output receipt = new Output ();
    receipt.setVisible(true);


}}

1 Answers1

3

You need to listen to changes on the JTextField.

Something like:

public class ListenerForTextField extends JFrame {
  JTextField text = new JTextField("Some Value");

  public ListenerForTextField () {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    text.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        //SomeOtherClass a = new SomeOtherClass();
        //a.useText(text.getText());
      }
    });
}
Community
  • 1
  • 1
MordechayS
  • 1,552
  • 2
  • 19
  • 29
  • @Charity Rama - If this answer or any other one solved your issue, please mark it as accepted. Welcome to Stack Overflow! – MordechayS Nov 21 '16 at 13:04