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