0

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

3
  • Are you sure that you're query is fast enough to handle such things? Commented 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. Commented Apr 8, 2011 at 7:51
  • 1) It is spelled Java, not JAVA. 2) What is your question? Commented Apr 8, 2011 at 8:31

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot Tom. I will try your suggestion, and give you a feedback once I'm done.
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.
Oh, so you were suggesting to create a timer for every task?
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 =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.