I have a problem with Java Textfield that is when I cover all text in the JTextField and input new text immediately(do not pass backspace) into the JTextField, then I use function getText() I get the previous string not current string. Please help for some solutions. Thanks in advance.
Share
I just tested the problem you described by adding a keyListener to a JTextField and printing the getText() method’s return value to the console.
What I found out is that it is always one character behind if you want to use the getText() method right in the keyTyped or keyPressed event (I didn’t know this because I usually just use a button to confirm I’m done entering the text and bind a KeyEvent to the Return key to trigger the button if a user wants to confirm by hitting enter)
I think this is due to the textField updating its text value AFTER the event is shot.
I assume this is what you did since you didn’t provide sample code, so I’ll delete this answer if it’s not.
The work around to this is to implement what you want to do in the keyReleased method instead.