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
-
Are you sure that you're query is fast enough to handle such things?RoflcoptrException– RoflcoptrException2011-04-08 07:36:45 +00:00Commented Apr 8, 2011 at 7:36
-
Even if there were an additional query overhead, what I really care about is waiting for 2 seconds before taking the input from the textbox and display it in my JTable.Mouhammed Soueidane– Mouhammed Soueidane2011-04-08 07:51:12 +00:00Commented Apr 8, 2011 at 7:51
-
1) It is spelled Java, not JAVA. 2) What is your question?Andrew Thompson– Andrew Thompson2011-04-08 08:31:15 +00:00Commented Apr 8, 2011 at 8:31
Add a comment
|
1 Answer
Try a java.util.Timer, on each key press cancel it and reschedule a TimerTask that's to do your query.
4 Comments
Mouhammed Soueidane
Thanks a lot Tom. I will try your suggestion, and give you a feedback once I'm done.
Mouhammed Soueidane
I have tried using the TimerTask and it's working (I'm getting the result after X amount of seconds). But let's say that I have a task that's still waiting for the delay time till it executes, and another task executes during that. How can I cancel the previous task completely so I could only execute the new task? I tried timer.cancel(), but this would disallow me to reuse the Timer class object.
Mouhammed Soueidane
Oh, so you were suggesting to create a timer for every task?
Mouhammed Soueidane
Nevermind, I have done the following logic: void run(){ timer.cancel(); timer=new Timer(); //Business logic goes here } This would cancel the previous timer, and create a new timer for the new task. Thanks for the help everyone =)