javax.swing
Class Timer

java.lang.Object
  extended by javax.swing.Timer
All Implemented Interfaces:
Serializable

public class Timer
extends Object
implements Serializable

Fires one or more action events after the specified delay. This is a specialised version of java.util.Timer just for firing ActionEvents. All Timers share one (daemon) Thread (or java.util.Timer). All events are fired from the event queue.

See Also:
Serialized Form

Field Summary
protected  EventListenerList listenerList
          A field to store all listeners who are listening to this timer.
 
Constructor Summary
Timer(int d, ActionListener listener)
          Creates a new Timer object.
 
Method Summary
 void addActionListener(ActionListener listener)
          Add the action listener
protected  void fireActionPerformed(ActionEvent event)
          Fire the given action event to the action listeners.
 ActionListener[] getActionListeners()
          Get the array of action listeners.
 int getDelay()
          Get the delay between firing the subsequent events.
 int getInitialDelay()
          Get the intial delay before firing the first event since calling the start() method.
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          Get the event listeners of the given type that are listening for the events, fired by this timer.
static boolean getLogTimers()
          Return the logging state.
 boolean isCoalesce()
          Checks if the Timer coalesces multiple pending event firings.
 boolean isRepeats()
          Check is this timer fires repetetive events.
 boolean isRunning()
          Get the timer state.
 void removeActionListener(ActionListener listener)
          Remove the action listener.
 void restart()
          Cancel all pending tasks and fire the first event after the initial delay.
 void setCoalesce(boolean c)
          Sets whether the Timer coalesces multiple pending event firings.
 void setDelay(int d)
          Set the delay between firing the subsequent events.
 void setInitialDelay(int i)
          Set the intial delay before firing the first event since calling the start() method.
static void setLogTimers(boolean lt)
          Set the timer logging state.
 void setRepeats(boolean r)
          Enable firing the repetetive events.
 void start()
          Start firing the action events.
 void stop()
          Stop firing the action events.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listenerList

protected EventListenerList listenerList
A field to store all listeners who are listening to this timer.

Constructor Detail

Timer

public Timer(int d,
             ActionListener listener)
Creates a new Timer object.

Parameters:
d - the default value for both initial and between event delay, in milliseconds.
listener - the first action listener, can be null.
Method Detail

getActionListeners

public ActionListener[] getActionListeners()
Get the array of action listeners.

Returns:
the array of action listeners that are listening for the events, fired by this timer
Since:
1.4

setCoalesce

public void setCoalesce(boolean c)
Sets whether the Timer coalesces multiple pending event firings. If the coalescing is enabled, the multiple events that have not been fired on time are replaced by the single event. The events may not be fired on time if the application is busy.

Parameters:
c - true (default) to enable the event coalescing, false otherwise

isCoalesce

public boolean isCoalesce()
Checks if the Timer coalesces multiple pending event firings. If the coalescing is enabled, the multiple events that have not been fired on time are replaced by the single event. The events may not be fired on time if the application is busy.

Returns:
true if the coalescing is enabled, false otherwise

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Get the event listeners of the given type that are listening for the events, fired by this timer.

Parameters:
listenerType - the listener type (for example, ActionListener.class)
Returns:
the array of event listeners that are listening for the events, fired by this timer
Since:
1.3

setLogTimers

public static void setLogTimers(boolean lt)
Set the timer logging state. If it is set to true, the timer prints a message to System.out when firing each action event.

Parameters:
lt - true if logging is enabled, false (default value) otherwise

getLogTimers

public static boolean getLogTimers()
Return the logging state.

Returns:
true if the timer is printing a message to System.out when firing each action event

setDelay

public void setDelay(int d)
Set the delay between firing the subsequent events. This parameter does not change the value of the initial delay before firing the first event.

Parameters:
d - The time gap between the subsequent events, in milliseconds
Throws:
IllegalArgumentException - if d is less than zero.

getDelay

public int getDelay()
Get the delay between firing the subsequent events.

Returns:
The delay between subsequent events, in milliseconds

setInitialDelay

public void setInitialDelay(int i)
Set the intial delay before firing the first event since calling the start() method. If the initial delay has not been set, it is assumed having the same value as the delay between the subsequent events.

Parameters:
i - the initial delay, in milliseconds
Throws:
IllegalArgumentException - if i is less than zero.

getInitialDelay

public int getInitialDelay()
Get the intial delay before firing the first event since calling the start() method. If the initial delay has not been set, returns the same value as getDelay().

Returns:
the initial delay before firing the first action event.

setRepeats

public void setRepeats(boolean r)
Enable firing the repetetive events.

Parameters:
r - true (default value) to fire repetetive events. false to fire only one event after the initial delay

isRepeats

public boolean isRepeats()
Check is this timer fires repetetive events.

Returns:
true if the timer fires repetetive events, false if it fires only one event after the initial delay

isRunning

public boolean isRunning()
Get the timer state.

Returns:
true if the timer has been started and is firing the action events as scheduled. false if the timer is inactive.

addActionListener

public void addActionListener(ActionListener listener)
Add the action listener

Parameters:
listener - the action listener to add

removeActionListener

public void removeActionListener(ActionListener listener)
Remove the action listener.

Parameters:
listener - the action listener to remove

restart

public void restart()
Cancel all pending tasks and fire the first event after the initial delay.


start

public void start()
Start firing the action events.


stop

public void stop()
Stop firing the action events.


fireActionPerformed

protected void fireActionPerformed(ActionEvent event)
Fire the given action event to the action listeners.

Parameters:
event - the event to fire