How can I call function after 5 seconds of last key pressed in java. Any Suggestions ?
Asked
Active
Viewed 284 times
-6
-
1Yes. Read [the Documentation](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – Boris the Spider Aug 24 '14 at 18:15
-
1possible duplicate of [java: run a function after a specific number of seconds](http://stackoverflow.com/questions/2258066/java-run-a-function-after-a-specific-number-of-seconds) – Michał Schielmann Aug 24 '14 at 18:16
-
@MichałSchielmann no, because that's outside of a Swing application - that example would violate Swing's threading policy. – Boris the Spider Aug 24 '14 at 18:25
-
1@BoristheSpider In the question that I've attached there is a Swing example. It also has link to `How to Use Swing Timers doc`. Is that not a valid example? Isn't that something to start with? – Michał Schielmann Aug 24 '14 at 18:31
-
@MichałSchielmann yes, there is answer there the is correct for this example but it is not the accepted answer to that question and the question itself is not about Swing. So it certainly isn't an exact duplicate of this question. Linking to the _relevant_ answer on that question might have been useful as a pointer for the OP. – Boris the Spider Aug 24 '14 at 18:34
-
@BoristheSpider ok then I could have flagged it as too broad or something. This question shows lack of effort from the OP. Would this be a proper duplicate: http://stackoverflow.com/questions/13503788/confusion-with-the-java-swing-timer? – Michał Schielmann Aug 24 '14 at 18:47
1 Answers
1
How can I call function after 5 seconds of last key pressed in java
This statement implies that the user might be pressing multiple keys and you only want to do the function 5 seconds after the user has stopped pressing keys.
So the solution would be to use a KeyListener on your component to listen for KeyEvents. Then whenever a key is press you restart a Swing Timer. Once the user stops pressing keys the Timer will generate and event and you can invoke your function.
For an example of this approach check out Application Inactivity. It uses this concept to listen for activity for the entire application (key events and mouse events).
Without more detail regarding your specify requirement we can only provide a general approach.

camickr
- 321,443
- 19
- 166
- 288