Search code examples
javadelaykeylistener

Java KeyListener with Delay


I need help implementing a new feature in an existing product that's implemented using JAVA. The new feature would allow users to search for items in a database, and display them in a Jtable. The search criteria will be typed in a textbox, and relevant results should appear in the table accordingly. I can easily create a "Submit" button to submit the search criteria and get the results. But what I really want is to implement a keylistener on the textbox that would wait around 2 seconds before submitting a query. For instance if a user inputs "D" and waits for 1 second, and then inputs "O", the submission of the query would happen 2 seconds after inputting "O" ( renewing the delay time upon each keystroke). Can


Solution

  • Try a java.util.Timer, on each key press cancel it and reschedule a TimerTask that's to do your query.