Search code examples
javajtextfieldprintstream

How to print out the input or JTextFiled


As a part of my program, I just want to print out what the user enters in the jTextField

here is what I do, but does not work at all.

JTextField myInput = new JTextField();
String word = myInput.getText();

myInput.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) 
{
System.out.print(word);
}

});

Any idea?


Solution

  • In the actionlistener you need to retrieve the value from the textfield.

     System.out.print(myInput.getText());
    

    At the moment, you are getting a empty value because at the point where you call getText() there is nothing in the textfield because the user did not have time to type something.