Results 1 to 7 of 7
Thread: [SOLVED] Swing Timer issue
- 03-02-2009, 03:53 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
[SOLVED] Swing Timer issue
Hi, is there any alternative to using Thread.sleep() to wait a certain amount of seconds within a swing timer? The program is not responsive at times using Thread.sleep(). I have heard its a bad idea with a GUI. I have posted code below.
Help is greatly appreciated.
javax.swing.Timer timer = new javax.swing.Timer(1000, new MyTimerActionListener());
timer.start();
try
{
Thread.sleep(8000);
}
catch (InterruptedException e)
{
}
timer.stop();
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyTimerActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
-
Not only is it a "bad idea", you shouldn't use thread.sleep at all in a GUI on the EDT code as it will only serve to put your app to sleep. Why not use a timer itself? That's what it's for after all.
If this doesn't help, then please give more information on just what exactly the program is supposed to be doing. Good luck.
- 03-03-2009, 09:05 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
Ok, so as you can see in the example I posted, all I want to do is wait a number of seconds. So,for example, how can I wait 8 seconds without using thread.sleep()?
- 03-03-2009, 10:54 AM #4
- 03-03-2009, 11:37 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
I have looked at the tutorial, but it didn't help. I just want the program to do nothing for x number of seconds. How do I do this?
- 03-03-2009, 11:53 AM #6
Timer#setInitialDelay, as you could have easily found out by going through the API.
And just looking at a tutorial is rarely useful, you need to actually do the exercises.
db
- 03-03-2009, 12:25 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
Inside a Timer thread loop,how to refresh a JTable in swing
By neha_negi in forum Threads and SynchronizationReplies: 3Last Post: 09-04-2009, 01:45 AM -
Java1.5 Swing Issue
By naveenbk in forum AWT / SwingReplies: 1Last Post: 11-23-2008, 08:03 PM -
EJB Timer
By mrjunsy in forum Advanced JavaReplies: 0Last Post: 08-22-2008, 04:09 PM -
EJB Timer
By mrjunsy in forum New To JavaReplies: 0Last Post: 08-04-2008, 06:47 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks