javax.swing
Class Timer

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by javax.swing.Timer
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable

public class Timer
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Serializable sample code for java.io.Serializable definition code for java.io.Serializable

Fires one or more action events after a specified delay. For example, an animation object can use a Timer as the trigger for drawing its frames.

Setting up a timer involves creating a Timer object, registering one or more action listeners on it, and starting the timer using the start method. For example, the following code creates and starts a timer that fires an action event once per second (as specified by the first argument to the Timer constructor). The second argument to the Timer constructor specifies a listener to receive the timer's action events.

  int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };
  new Timer(delay, taskPerformer).start();

Each Timer has one or more action listeners and a delay (the time between action events). When delay milliseconds have passed, the Timer fires an action event to its listeners. By default, this cycle repeats until the stop method is called. If you want the timer to fire only once, invoke setRepeats(false) on the timer. To make the delay before the first action event different from the delay between events, use the setInitialDelay method.

Although all Timers perform their waiting using a single, shared thread (created by the first Timer object that executes), the action event handlers for Timers execute on another thread -- the event-dispatching thread. This means that the action handlers for Timers can safely perform operations on Swing components. However, it also means that the handlers must execute quickly to keep the GUI responsive.

In v 1.3, another Timer class was added to the Java platform: java.util.Timer. Both it and javax.swing.Timer provide the same basic functionality, but java.util.Timer is more general and has more features. The javax.swing.Timer has two features that can make it a little easier to use with GUIs. First, its event handling metaphor is familiar to GUI programmers and can make dealing with the event-dispatching thread a bit simpler. Second, its automatic thread sharing means that you don't have to take special steps to avoid spawning too many threads. Instead, your timer uses the same thread used to make cursors blink, tool tips appear, and so on.

You can find further documentation and several examples of using timers by visiting How to Use Timers, a section in The Java Tutorial. For more examples and help in choosing between this Timer class and java.util.Timer, see Using Timers in Swing Applications, an article in The Swing Connection.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder sample code for java.beans.XMLEncoder definition code for java.beans.XMLEncoder .

See Also:
java.util.Timer sample code for java.util.Timer definition code for java.util.Timer , Serialized Form

Field Summary
protected  EventListenerList sample code for javax.swing.event.EventListenerList definition code for javax.swing.event.EventListenerList listenerList sample code for javax.swing.Timer.listenerList definition code for javax.swing.Timer.listenerList
           
 
Constructor Summary
Timer sample code for javax.swing.Timer.Timer(int, java.awt.event.ActionListener) definition code for javax.swing.Timer.Timer(int, java.awt.event.ActionListener) (int delay, ActionListener sample code for java.awt.event.ActionListener definition code for java.awt.event.ActionListener  listener)
          Creates a Timer that will notify its listeners every delay milliseconds.
 
Method Summary
 void addActionListener sample code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) (ActionListener sample code for java.awt.event.ActionListener definition code for java.awt.event.ActionListener  listener)
          Adds an action listener to the Timer.
protected  void fireActionPerformed sample code for javax.swing.Timer.fireActionPerformed(java.awt.event.ActionEvent) definition code for javax.swing.Timer.fireActionPerformed(java.awt.event.ActionEvent) (ActionEvent sample code for java.awt.event.ActionEvent definition code for java.awt.event.ActionEvent  e)
          Notifies all listeners that have registered interest for notification on this event type.
 ActionListener sample code for java.awt.event.ActionListener definition code for java.awt.event.ActionListener [] getActionListeners sample code for javax.swing.Timer.getActionListeners() definition code for javax.swing.Timer.getActionListeners() ()
          Returns an array of all the action listeners registered on this timer.
 int getDelay sample code for javax.swing.Timer.getDelay() definition code for javax.swing.Timer.getDelay() ()
          Returns the delay, in milliseconds, between firings of action events.
 int getInitialDelay sample code for javax.swing.Timer.getInitialDelay() definition code for javax.swing.Timer.getInitialDelay() ()
          Returns the Timer's initial delay.
<T extends EventListener sample code for java.util.EventListener definition code for java.util.EventListener >
T[]
getListeners sample code for javax.swing.Timer.getListeners(java.lang.Class) definition code for javax.swing.Timer.getListeners(java.lang.Class) (Class sample code for java.lang.Class definition code for java.lang.Class <T> listenerType)
          Returns an array of all the objects currently registered as FooListeners upon this Timer.
static boolean getLogTimers sample code for javax.swing.Timer.getLogTimers() definition code for javax.swing.Timer.getLogTimers() ()
          Returns true if logging is enabled.
 boolean isCoalesce sample code for javax.swing.Timer.isCoalesce() definition code for javax.swing.Timer.isCoalesce() ()
          Returns true if the Timer coalesces multiple pending action events.
 boolean isRepeats sample code for javax.swing.Timer.isRepeats() definition code for javax.swing.Timer.isRepeats() ()
          Returns true (the default) if the Timer will send an action event to its listeners multiple times.
 boolean isRunning sample code for javax.swing.Timer.isRunning() definition code for javax.swing.Timer.isRunning() ()
          Returns true if the Timer is running.
 void removeActionListener sample code for javax.swing.Timer.removeActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.removeActionListener(java.awt.event.ActionListener) (ActionListener sample code for java.awt.event.ActionListener definition code for java.awt.event.ActionListener  listener)
          Removes the specified action listener from the Timer.
 void restart sample code for javax.swing.Timer.restart() definition code for javax.swing.Timer.restart() ()
          Restarts the Timer, canceling any pending firings and causing it to fire with its initial delay.
 void setCoalesce sample code for javax.swing.Timer.setCoalesce(boolean) definition code for javax.swing.Timer.setCoalesce(boolean) (boolean flag)
          Sets whether the Timer coalesces multiple pending ActionEvent firings.
 void setDelay sample code for javax.swing.Timer.setDelay(int) definition code for javax.swing.Timer.setDelay(int) (int delay)
          Sets the Timer's delay, the number of milliseconds between successive action events.
 void setInitialDelay sample code for javax.swing.Timer.setInitialDelay(int) definition code for javax.swing.Timer.setInitialDelay(int) (int initialDelay)
          Sets the Timer's initial delay, which by default is the same as the between-event delay.
static void setLogTimers sample code for javax.swing.Timer.setLogTimers(boolean) definition code for javax.swing.Timer.setLogTimers(boolean) (boolean flag)
          Enables or disables the timer log.
 void setRepeats sample code for javax.swing.Timer.setRepeats(boolean) definition code for javax.swing.Timer.setRepeats(boolean) (boolean flag)
          If flag is false, instructs the Timer to send only one action event to its listeners.
 void start sample code for javax.swing.Timer.start() definition code for javax.swing.Timer.start() ()
          Starts the Timer, causing it to start sending action events to its listeners.
 void stop sample code for javax.swing.Timer.stop() definition code for javax.swing.Timer.stop() ()
          Stops the Timer, causing it to stop sending action events to its listeners.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

listenerList sample code for javax.swing.Timer.listenerList

protected EventListenerList sample code for javax.swing.event.EventListenerList definition code for javax.swing.event.EventListenerList  listenerList
Constructor Detail

Timer sample code for javax.swing.Timer(int, java.awt.event.ActionListener) definition code for javax.swing.Timer(int, java.awt.event.ActionListener)

public Timer(int delay,
             ActionListener sample code for java.awt.event.ActionListener definition code for java.awt.event.ActionListener  listener)
Creates a Timer that will notify its listeners every delay milliseconds. If delay is less than or equal to zero the timer will fire as soon as it is started. If listener is not null, it's registered as an action listener on the timer.

Parameters:
delay - the number of milliseconds between action events
listener - an initial listener; can be null
See Also:
addActionListener(java.awt.event.ActionListener) sample code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) , setInitialDelay(int) sample code for javax.swing.Timer.setInitialDelay(int) definition code for javax.swing.Timer.setInitialDelay(int) , setRepeats(boolean) sample code for javax.swing.Timer.setRepeats(boolean) definition code for javax.swing.Timer.setRepeats(boolean)
Method Detail

addActionListener sample code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener)

public void addActionListener(ActionListener sample code for java.awt.event.ActionListener definition code for java.awt.event.ActionListener  listener)
Adds an action listener to the Timer.

Parameters:
listener - the listener to add
See Also:
Timer(int, java.awt.event.ActionListener) sample code for javax.swing.Timer.Timer(int, java.awt.event.ActionListener) definition code for javax.swing.Timer.Timer(int, java.awt.event.ActionListener)

removeActionListener sample code for javax.swing.Timer.removeActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.removeActionListener(java.awt.event.ActionListener)

public void removeActionListener(ActionListener sample code for java.awt.event.ActionListener definition code for java.awt.event.ActionListener  listener)
Removes the specified action listener from the Timer.

Parameters:
listener - the listener to remove

getActionListeners sample code for javax.swing.Timer.getActionListeners() definition code for javax.swing.Timer.getActionListeners()

public ActionListener sample code for java.awt.event.ActionListener definition code for java.awt.event.ActionListener [] getActionListeners()
Returns an array of all the action listeners registered on this timer.

Returns:
all of the timer's ActionListeners or an empty array if no action listeners are currently registered
Since:
1.4
See Also:
addActionListener(java.awt.event.ActionListener) sample code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) , removeActionListener(java.awt.event.ActionListener) sample code for javax.swing.Timer.removeActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.removeActionListener(java.awt.event.ActionListener)

fireActionPerformed sample code for javax.swing.Timer.fireActionPerformed(java.awt.event.ActionEvent) definition code for javax.swing.Timer.fireActionPerformed(java.awt.event.ActionEvent)

protected void fireActionPerformed(ActionEvent sample code for java.awt.event.ActionEvent definition code for java.awt.event.ActionEvent  e)
Notifies all listeners that have registered interest for notification on this event type.

Parameters:
e - the action event to fire
See Also:
EventListenerList sample code for javax.swing.event.EventListenerList definition code for javax.swing.event.EventListenerList

getListeners sample code for javax.swing.Timer.<T extends java.util.EventListener>getListeners(java.lang.Class<T>) definition code for javax.swing.Timer.<T extends java.util.EventListener>getListeners(java.lang.Class<T>)

public <T extends EventListener sample code for java.util.EventListener definition code for java.util.EventListener > T[] getListeners(Class sample code for java.lang.Class definition code for java.lang.Class <T> listenerType)
Returns an array of all the objects currently registered as FooListeners upon this Timer. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a Timer instance t for its action listeners with the following code:

ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));
If no such listeners exist, this method returns an empty array.

Parameters:
listenerType - the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
Returns:
an array of all objects registered as FooListeners on this timer, or an empty array if no such listeners have been added
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if listenerType doesn't specify a class or interface that implements java.util.EventListener
Since:
1.3
See Also:
getActionListeners() sample code for javax.swing.Timer.getActionListeners() definition code for javax.swing.Timer.getActionListeners() , addActionListener(java.awt.event.ActionListener) sample code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.addActionListener(java.awt.event.ActionListener) , removeActionListener(java.awt.event.ActionListener) sample code for javax.swing.Timer.removeActionListener(java.awt.event.ActionListener) definition code for javax.swing.Timer.removeActionListener(java.awt.event.ActionListener)

setLogTimers sample code for javax.swing.Timer.setLogTimers(boolean) definition code for javax.swing.Timer.setLogTimers(boolean)

public static void setLogTimers(boolean flag)
Enables or disables the timer log. When enabled, a message is posted to System.out whenever the timer goes off.

Parameters:
flag - true to enable logging
See Also:
getLogTimers() sample code for javax.swing.Timer.getLogTimers() definition code for javax.swing.Timer.getLogTimers()

getLogTimers sample code for javax.swing.Timer.getLogTimers() definition code for javax.swing.Timer.getLogTimers()

public static boolean getLogTimers()
Returns true if logging is enabled.

Returns:
true if logging is enabled; otherwise, false
See Also:
setLogTimers(boolean) sample code for javax.swing.Timer.setLogTimers(boolean) definition code for javax.swing.Timer.setLogTimers(boolean)

setDelay sample code for javax.swing.Timer.setDelay(int) definition code for javax.swing.Timer.setDelay(int)

public void setDelay(int delay)
Sets the Timer's delay, the number of milliseconds between successive action events.

Parameters:
delay - the delay in milliseconds
See Also:
setInitialDelay(int) sample code for javax.swing.Timer.setInitialDelay(int) definition code for javax.swing.Timer.setInitialDelay(int)

getDelay sample code for javax.swing.Timer.getDelay() definition code for javax.swing.Timer.getDelay()

public int getDelay()
Returns the delay, in milliseconds, between firings of action events.

See Also:
setDelay(int) sample code for javax.swing.Timer.setDelay(int) definition code for javax.swing.Timer.setDelay(int) , getInitialDelay() sample code for javax.swing.Timer.getInitialDelay() definition code for javax.swing.Timer.getInitialDelay()

setInitialDelay sample code for javax.swing.Timer.setInitialDelay(int) definition code for javax.swing.Timer.setInitialDelay(int)

public void setInitialDelay(int initialDelay)
Sets the Timer's initial delay, which by default is the same as the between-event delay. This is used only for the first action event. Subsequent action events are spaced using the delay property.

Parameters:
initialDelay - the delay, in milliseconds, between the invocation of the start method and the first action event fired by this timer
See Also:
setDelay(int) sample code for javax.swing.Timer.setDelay(int) definition code for javax.swing.Timer.setDelay(int)

getInitialDelay sample code for javax.swing.Timer.getInitialDelay() definition code for javax.swing.Timer.getInitialDelay()

public int getInitialDelay()
Returns the Timer's initial delay.

See Also:
setInitialDelay(int) sample code for javax.swing.Timer.setInitialDelay(int) definition code for javax.swing.Timer.setInitialDelay(int) , setDelay(int) sample code for javax.swing.Timer.setDelay(int) definition code for javax.swing.Timer.setDelay(int)

setRepeats sample code for javax.swing.Timer.setRepeats(boolean) definition code for javax.swing.Timer.setRepeats(boolean)

public void setRepeats(boolean flag)
If flag is false, instructs the Timer to send only one action event to its listeners.

Parameters:
flag - specify false to make the timer stop after sending its first action event

isRepeats sample code for javax.swing.Timer.isRepeats() definition code for javax.swing.Timer.isRepeats()

public boolean isRepeats()
Returns true (the default) if the Timer will send an action event to its listeners multiple times.

See Also:
setRepeats(boolean) sample code for javax.swing.Timer.setRepeats(boolean) definition code for javax.swing.Timer.setRepeats(boolean)

setCoalesce sample code for javax.swing.Timer.setCoalesce(boolean) definition code for javax.swing.Timer.setCoalesce(boolean)

public void setCoalesce(boolean flag)
Sets whether the Timer coalesces multiple pending ActionEvent firings. A busy application may not be able to keep up with a Timer's event generation, causing multiple action events to be queued. When processed, the application sends these events one after the other, causing the Timer's listeners to receive a sequence of events with no delay between them. Coalescing avoids this situation by reducing multiple pending events to a single event. Timers coalesce events by default.

Parameters:
flag - specify false to turn off coalescing

isCoalesce sample code for javax.swing.Timer.isCoalesce() definition code for javax.swing.Timer.isCoalesce()

public boolean isCoalesce()
Returns true if the Timer coalesces multiple pending action events.

See Also:
setCoalesce(boolean) sample code for javax.swing.Timer.setCoalesce(boolean) definition code for javax.swing.Timer.setCoalesce(boolean)

start sample code for javax.swing.Timer.start() definition code for javax.swing.Timer.start()

public void start()
Starts the Timer, causing it to start sending action events to its listeners.

See Also:
stop() sample code for javax.swing.Timer.stop() definition code for javax.swing.Timer.stop()

isRunning sample code for javax.swing.Timer.isRunning() definition code for javax.swing.Timer.isRunning()

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

See Also:
start() sample code for javax.swing.Timer.start() definition code for javax.swing.Timer.start()

stop sample code for javax.swing.Timer.stop() definition code for javax.swing.Timer.stop()

public void stop()
Stops the Timer, causing it to stop sending action events to its listeners.

See Also:
start() sample code for javax.swing.Timer.start() definition code for javax.swing.Timer.start()

restart sample code for javax.swing.Timer.restart() definition code for javax.swing.Timer.restart()

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