jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Timer problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Timer problem" Watch "Timer problem" New topic
Author

Timer problem

Michael Jameston
Greenhorn

Joined: Sep 25, 2009
Posts: 4
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().

Thanks

Here is the complete code.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
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.
Michael Jameston
Greenhorn

Joined: Sep 25, 2009
Posts: 4
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?

Thanks

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
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:

panel.revalidate();
panel.repaint();

The whole point of using a Timer is to schedule some activity in the future. It is not to stop current code from executing.




 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Timer problem
 
Similar Threads
JLabel appears as a JButton
Setting color in a JComboBox
Help i need help with borderlayout
GridBagLayout Question: Irregular Spacing
Some help.