net.sf.ecl.datepicker.util
Class Timer

java.lang.Object
  extended by net.sf.ecl.datepicker.util.Timer

public class Timer
extends Object

Causes the run() method of runnables to be invoked by the user-interface thread after the specified delay and period milliseconds have elapsed.

Setting up a timer involves creating a Timer object, registering one or more runnables on it, and starting the timer using the start method. For example, the following code creates and starts a timer that runs a runnable one per second (as specified by the first argument to the Timer constructor). The second argument to the Timer contructor specifies a runnable to run.

         int period = 1000; // milliseconds
         Runnable runnable = new Runnable() {
             public void run() {
 
 // ...Perform what you want...
 
             }
         }
         (new Timer(period, runnable)).start();
 

Each Timer has one or more runnables and a period. When period milliseconds have passed, the Timer run its runnables. By default, this cycle repeats until the stop method is called. If you want the timer to run only once, invoke setRepeats(false) or setPeriod(0) on the timer. To make the delay before the first run different from the period, use setDelay method or Timer(delay, period, runnable) constructor.


Field Summary
static int DAY
          Number of milliseconds in one day.
static int HOUR
          Number of milliseconds in one hour.
static int MINUTE
          Number of milliseconds in one minute.
static int SECOND
          Number of milliseconds in one second.
 
Constructor Summary
Timer(int delay, int period, Runnable runnable)
          Creates a Timer that will run its runnables after delay and than every period milliseconds.
Timer(int period, Runnable runnable)
          Creates a Timer that will run its runnables every period milliseconds.
 
Method Summary
 void addRunnable(Runnable runnable)
          Adds the given runnable to this timer to run when ringing.
 int getDelay()
          Returns the Timer's delay.
 int getPeriod()
          Returns the Timer's period, in milliseconds, between running its runnables.
 boolean isRepeats()
          Returns true (the default) if the Timer will run its runnables multiple times.
 boolean isRunning()
          Returns true if the Timer is running.
 void removeRunnable(Runnable runnable)
          Removes the given runnable from this timer.
 void restart()
          Restarts the Timer, canceling any pending runnings and causing it to run with its delay.
 void setDelay(int delay)
          Sets the Timer's delay, which by default is the same as the period.
 void setPeriod(int period)
          Sets the Timer's period, the number of milliseconds between successive runnings of its runnables.
 void setRepeats(boolean repeats)
          if repeats is false, instructs the Timer to run its runnables only one or to stop if already running.
 void start()
          Starts the Timer, causing it to start running its runnables.
 void stop()
          Stops the Timer, causing it to stop running its runnables.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SECOND

public static final int SECOND
Number of milliseconds in one second.

See Also:
Constant Field Values

MINUTE

public static final int MINUTE
Number of milliseconds in one minute.

See Also:
Constant Field Values

HOUR

public static final int HOUR
Number of milliseconds in one hour.

See Also:
Constant Field Values

DAY

public static final int DAY
Number of milliseconds in one day.

See Also:
Constant Field Values
Constructor Detail

Timer

public Timer(int delay,
             int period,
             Runnable runnable)
Creates a Timer that will run its runnables after delay and than every period milliseconds.

Parameters:
delay - the delay, in milliseconds, between the start method and the first runnables running by this timer
period - the number of milliseconds between runnables running
runnable - an initial runnable, can be null

Timer

public Timer(int period,
             Runnable runnable)
Creates a Timer that will run its runnables every period milliseconds.

Parameters:
period - the number of milliseconds between runnables running
runnable - an initial runnable, can be null
Method Detail

addRunnable

public void addRunnable(Runnable runnable)
Adds the given runnable to this timer to run when ringing.

Parameters:
runnable - the runnable to add

getDelay

public int getDelay()
Returns the Timer's delay.

Returns:
the Timer's delay.
See Also:
setDelay(int), getPeriod()

getPeriod

public int getPeriod()
Returns the Timer's period, in milliseconds, between running its runnables.

Returns:
the period in milliseconds
See Also:
setPeriod(int)

isRepeats

public boolean isRepeats()
Returns true (the default) if the Timer will run its runnables multiple times.

Returns:
true if is repeats; otherwise, false
See Also:
setRepeats(boolean), setPeriod(int)

isRunning

public boolean isRunning()
Returns true if the Timer is running.

Returns:
true if is running; otherwise, false
See Also:
start(), stop()

removeRunnable

public void removeRunnable(Runnable runnable)
Removes the given runnable from this timer.

Parameters:
runnable - the runnable to remove

restart

public void restart()
Restarts the Timer, canceling any pending runnings and causing it to run with its delay.


setDelay

public void setDelay(int delay)
Sets the Timer's delay, which by default is the same as the period. This is used only for the first running. Subsequent running are spaced using the period property.

Parameters:
delay - the delay, in milliseconds, between the start method and the first runnables running by this timer
See Also:
getDelay(), setPeriod(int)

setPeriod

public void setPeriod(int period)
Sets the Timer's period, the number of milliseconds between successive runnings of its runnables. If the given period is zero and the Timer is repeats, then setRepeats(false) will be invoked.

Parameters:
period - the period in milliseconds
See Also:
getPeriod(), setDelay(int)

setRepeats

public void setRepeats(boolean repeats)
if repeats is false, instructs the Timer to run its runnables only one or to stop if already running.

Parameters:
repeats - specify false to make the timer stop after first running its runnables
See Also:
isRepeats(), setPeriod(int)

start

public void start()
Starts the Timer, causing it to start running its runnables.

See Also:
stop()

stop

public void stop()
Stops the Timer, causing it to stop running its runnables.

See Also:
start()