FalcorTheDog
12-09-2004, 09:19 AM
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?
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() );
}
}
shmoove
12-09-2004, 10:43 AM
Well, you're starting the Timer, but you're stopping it immediately after that. Try waiting a bit before you stop the Timer:
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
FalcorTheDog
12-09-2004, 10:24 PM
Awesome, I got it working, but could you explain this piece of code to me?:
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:
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!
Jason
12-09-2004, 11:44 PM
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
FalcorTheDog
12-09-2004, 11:53 PM
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):
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!
Jason
12-10-2004, 12:08 AM
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
shmoove
12-12-2004, 08:38 AM
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
CodeJibblets
12-28-2007, 10:35 AM
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; :thumbsup: 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.