Reputation: 61
I have the following problem. I create a JTextField
JTextField t = new JTextField("2");
and then add a listener.
t.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
System.out.println(t.getText());
}
});
But the getText()
doesn't return the new text.
For example, if I type 5, getText()
still returns 2, the old text.
Upvotes: 2
Views: 52
Reputation: 23012
You should use keyReleased
instead of keyPressed
method of KeyAdapter
and you will get the updated value.
Currently, keyPressed
method will be fired before the text gets updated in your text field. You should get the value when user release the key.
Upvotes: 3