Thread: Java Timer
View Single Post
Old 12-09-2004, 10:24 PM   PM User | #3
FalcorTheDog
New to the CF scene

 
Join Date: Dec 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
FalcorTheDog is an unknown quantity at this point
Awesome, I got it working, but could you explain this piece of code to me?:

Code:
   try 
      {
      Thread.sleep(10000);
      }
   catch (Exception e) {}
I was using the test.stop() just to play with the timer functions. If I take it out, it still won't work. So I guess my question is, why won't it work (display "test" every second) just like this:


Code:
import javax.swing.Timer;
import java.awt.event.*;

public class TimerTest
{

   public static void main( String args[] )
   {
   int delay = 1000; //milliseconds

   ActionListener taskPerformer = new ActionListener()
      {
      public void actionPerformed(ActionEvent evt)
         {
         System.out.println( "test" );
         }
      };

   Timer test = new Timer(delay, taskPerformer);

   test.start();

   System.out.println( test.isRunning() );
   }

}
The timer appears to be running, it just won't fire the action without that try/catch piece of code. Can anyone explain this to me? Thanks!
FalcorTheDog is offline   Reply With Quote