Reputation:
So I have this paradox where I have a mouseListener added to my mainframe. When mouse entered this has a loop to check if the time that passed is exceeding a certain limit so it is registered a hold. It has to be in a thread because otherwise, I could not check for clicks as my main Thread would be blocked (or would it?). But the problem is that I want to show info while the mouse Button is still clicked requiring me to call repaint from within the thread, which won't work as repaint only works from the main Thread, yet this one has to be free for the MouseListener... Does anyone have an idea how to solve this Problem?
Upvotes: -1
Views: 99
Reputation: 324108
When mouse entered this has a loop to check if the time that passed is exeeding a certain limit
Don't use a loop. If this is executed in the listener then you will be blocking the Event Dispatch Thread (EDT).
Instead use a Swing Timer. When you enter the component you start the Timer. The Timer will then generate an event after your specified time interval.
However, you can also stop the Timer if some other event is generated and you want to reset the timer.
This will not block the Event Dispatch Thread (EDT)
and events will still be generated normally.
i want to show info while the mouse Button is still clicked
Not sure what "still clicked" means. If the button is still pressed and you are executing code from an ActionListener you will block the EDT and the GUI won't be able to repaint itself until the long running task is completed.
Read the section from the Swing tutorial on Concurrency for more information about the EDT
.
Upvotes: 1