Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Java Timer

  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Timer

    I just started playing with timers today, so I'm not too sure what I'm doing. All I want is to get something simple to work... Here I'm trying to display "test" once every second. Why isn't it working? The timer appears to be running, but the actionevent isn't working. Any ideas?



    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() );
       System.out.println( test.getDelay() );
    
       test.stop();
       System.out.println( test.isRunning() );
    
       }
    
    }
    Last edited by FalcorTheDog; 12-09-2004 at 10:25 AM.

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, you're starting the Timer, but you're stopping it immediately after that. Try waiting a bit before you stop the Timer:
    Code:
      test.start();
    
       System.out.println( test.isRunning() );
       System.out.println( test.getDelay() );
    
       // wait 10 seconds before going on.   
       try {
          Thread.sleep(10000);
       }
       catch (Exception e) {}
    
       test.stop();
    You see, the code in the main thread is independent from the Timer, it runs on a different thread. So the main method just continues on after you start the Timer and immediately stops it, before one second passes and the event is fired for the first time.

    shmoove

  • #3
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the 'try' & 'catch' statements are an error handleing section of code, if something can possibly throw an error its best inside those and if something goes wrong you catch it and then tell it what to do, in your case you do nothing. the magic is with the Thread.sleep(1000) which is saying that you want to basically wait for 1000milisconds and then wake up...make sense?


    Jason

  • #5
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Neat, I sort of get it. But is there any way to do it without these try/catch statements? For example, why doesn't this simple code work (by "work" I mean displaying "test" every second):

    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() );
       }
    
    }
    Thanks for everyone's help!

  • #6
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, when you say test.start( ); you have only started the timer but instead of printing test you call System.println(test.isRunning() ); which will give you a boolean answer, not "test" like you mention. and thats the end....no more code.... you need the Thread.sleep(1000); to tie the two together and cause your action, printing "test" with your Timer...


    Jason

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That doesn't work because the program ends as soon as the main method finishes. Since the timer is just a local variable in the main method, as soon as the main method ends, there are no active references to it, so the garbage collection cleans it up.

    shmoove

  • #8
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding value to this thread

    For Timer you need at least the following:

    1) Add your import APIs
    import java.util.*; // need this for the timer "java.util.Timer;" works too
    import java.awt.event.*; // need this for your actionListener "java.awt.event.ActionListener;" works too)

    2) Create your Timer (Milliseconds, action) *this is encapsulated code to reduce wordage; 500 below is wait in Milliseconds- FYI
    Timer timer = new Timer(500, new ActionListener() {
    public void actionToDo(ActionEvent e) {
    //Your action code here
    }
    }); // closes timer's loaded constructor

    3)Start your timer
    timer.start(); // start now, wait 500 milliseconds and do my action code!
    *timer.stop(); // stops your timer

    I used this to change name of button after it was pressed then return to the orginal label. *timer.stop() wasn't required for a one time action event; good to use in a cycling function.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •