Results 1 to 2 of 2
  1. #1
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with timer in java

    Hi, 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.
    Java 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() );
    
       }
    
    }
    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import javax.swing.Timer;
    import java.awt.event.*;
    
    public class TimerTestRx
    {
        public static void main( String args[] )
        {
            int delay = 1000; //milliseconds
       
            ActionListener taskPerformer = new ActionListener()
            {
                int count = 0;
                public void actionPerformed(ActionEvent evt)
                {
                    System.out.println( "count = " + count++ );
                    if(count > 10) {
                        ((Timer)evt.getSource()).stop();
                        System.exit(0);
                    }
                }
            };
    
            Timer test = new Timer(delay, taskPerformer);
    
            test.start();
    
            System.out.println( test.isRunning() );
            System.out.println( test.getDelay() );
    
            // Can't do much after we get to this statement.
    //        test.stop();
            System.out.println( test.isRunning() );
            String s = "<html>We need something to keep the jvm from<br>" +
                       "exiting while we look for some timer activity";
            javax.swing.JOptionPane.showMessageDialog(null, s);
        }
    }

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  2. Timer usage in java program
    By sandeeprao.techno in forum New To Java
    Replies: 5
    Last Post: 01-24-2008, 07:16 PM
  3. Simple timer
    By im-not-alive in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:04 PM
  4. making a count down timer using java
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 09:49 PM
  5. Problem with timer in java
    By paul in forum Advanced Java
    Replies: 3
    Last Post: 07-26-2007, 10:18 AM

Posting Permissions

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