Yan Ivan Evdokimov
Yan Ivan Evdokimov

Reputation: 179

Disable keylistener in java

In my program i have keyListener set on my canvas. It is working great, but when I do Thread.sleep(1000) keyListener is still listening and reading keys, but result shows only when sleep time has ended. How to make keyListener not to read or listen keys typed in sleep period?

Upvotes: 0

Views: 4911

Answers (1)

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

Don't do Thread.sleep() with Swing. From the question, I deduct you are doing everything at EDT (Event Dispatch Thread). So if you put EDT to sleep, it won't disable Swing, it will just pause these events for a second.

You can set up a Timer for one second, and when you need to stop receiving key events, just disable the component by setEnabled(false), then start the timer, and when it runs out, you call setEnabled(true).

Upvotes: 4

Related Questions