Search code examples
javaswinguser-interfacejtextfield

Java - How to tell if user has entered text in a text field, but is now idle?


For a program I am writing, I need to be able to tell if the user has entered text in a text field (but is not currently typing). I have an action listener for the text field that is able to tell whether the user is currently typing via a KeyListener, but is there a way that I can tell if the user has stopped typing but has entered text in the text box?

Thanks!


Solution

  • You can use a Swing timer and regularly check if it was some time since the last key press in the field.

    E.g. for every key press, save a timestamp in a variable. Set the timer to check if the timestamp is older than e.g. 10 seconds.