Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 6 votes, 3.50 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2004, 09:19 AM   PM User | #1
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
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 09:25 AM..
FalcorTheDog is offline   Reply With Quote
Old 12-09-2004, 10:43 AM   PM User | #2
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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
shmoove is offline   Reply With Quote
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
Old 12-09-2004, 11:44 PM   PM User | #4
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
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
Jason is offline   Reply With Quote
Old 12-09-2004, 11:53 PM   PM User | #5
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
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!
FalcorTheDog is offline   Reply With Quote
Old 12-10-2004, 12:08 AM   PM User | #6
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
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
Jason is offline   Reply With Quote
Old 12-12-2004, 08:38 AM   PM User | #7
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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
shmoove is offline   Reply With Quote
Old 12-28-2007, 10:35 AM   PM User | #8
CodeJibblets
New to the CF scene

 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
CodeJibblets is an unknown quantity at this point
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.
CodeJibblets is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:04 AM.


Advertisement
Log in to turn off these ads.