aspose file tools*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 2177
    
    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: 2177
    
    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.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer problem