Here is my problem I have created a timer using the following code
The problem is that when this bit of code runs the ActionListener does not automatically run, what does happen however is that the listener I am currently in (ButtonListener) finishes and then the program enters timer listener. Could someone please point me in the right direction of how to get the desired behavior of an automatic jump when I call t.start().
If you want something to execute right away, then why are you using a Timer. Just execute the code directly.
All code in event listener executes "sequentially" in the EDT. So the Timer code can't be executed until the button ActionListener has finished executing.
Joined: Sep 25, 2009
Thanks for the quick response. I was unaware of the fact that an actionListener will not turn over control to another until finished. I have amended the code and now I do not call t.start() until the end of the button listener. However I now have the problem that when I am in the Timer listener for some reason the frame does not update when I change the labels in my panel and add it back to the frame. In this code the "IM THINKING" part never gets printed,Any thoughts?
Joined: Jun 13, 2009
Your design is still wrong. I don't understand what you are trying to do so I can't really make a suggestion. Some general comments.
a) Thread.sleep() is executed from withing the ActionListener, therefore you are causing the EDT to sleep, which means the GUI can't repaint itself or respond to events.
b) after you add/remove components from a visible GUI you need to do:
The whole point of using a Timer is to schedule some activity in the future. It is not to stop current code from executing.