ActionEvents at specified
intervals. An example use is an animation object that uses 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();
Timers are constructed by specifying both a delay parameter
and an ActionListener. The delay parameter is used
to set both the initial delay and the delay between event
firing, in milliseconds. Once the timer has been started,
it waits for the initial delay before firing its
first ActionEvent to registered listeners.
After this first event, it continues to fire events
every time the between-event delay has elapsed, until it
is stopped.
After construction, the initial delay and the between-event
delay can be changed independently, and additional
ActionListeners may be added.
If you want the timer to fire only the first time and then stop,
invoke setRepeats(false) on the timer.
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.
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
has been added to the java.beans package.
Please see java..
java.util.Timer
| Modifier and Type | Class and Description |
|---|---|
| pack-priv class | Timer.
DoPostEvent is a runnable class that fires actionEvents to the listeners on the EventDispatchThread, via invokeLater. |
| Modifier and Type | Field and Description |
|---|---|
| private volatile String | |
| private volatile boolean | |
| private volatile int | |
| pack-priv transient TimerQueue. | |
| private final transient Runnable | |
| private volatile int | |
| protected EventListenerList | listenerList
The collection of registered listeners |
| private final transient Lock | |
| private static volatile boolean | |
| private final transient AtomicBoolean | |
| private volatile boolean |
| Access | Constructor and Description |
|---|---|
| public | Timer(int
milliseconds for the initial and between-event delay delay, ActionListener an initial listener; can be listener)nullCreates a |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| pack-priv void | cancelEvent()
Resets the internal state to indicate this Timer shouldn't notify any of its listeners. |
| private static void | |
| protected void | fireActionPerformed(ActionEvent
the action event to fire e)Notifies all listeners that have registered interest for notification on this event type. |
| public String | Returns: the action command used in firing eventsReturns the string that will be delivered as the action command
in |
| public ActionListener[] | Returns: all of the timer'sActionListeners or an empty
array if no action listeners are currently registeredReturns an array of all the action listeners registered on this timer. |
| public int | Returns: the delay, in milliseconds, between firings of action eventsReturns the delay, in milliseconds, between firings of action events. |
| public int | Returns: theTimer's initial delay, in millisecondsReturns the |
| public < the type of T extends EventListener> T[]EventListener class being requested | Returns: an array of all objects registered asFooListeners
on this timer,
or an empty array if no such
listeners have been addedthe type of listeners requested;
this parameter should specify an interface
that descends from listenerType)java.util.EventListenerReturns an array of all the objects currently registered as
|
| pack-priv Lock | |
| public static boolean | Returns: true if logging is enabled; otherwise, falseReturns |
| public boolean | Returns: true if theTimer coalesces multiple pending
action eventsReturns |
| public boolean | Returns: true if theTimer will send an action event to its
listeners multiple timesReturns |
| public boolean | Returns: true if theTimer is running, false otherwiseReturns |
| pack-priv void | |
| private void | |
| private Object | |
| public void | removeActionListener(ActionListener
the listener to remove listener)Removes the specified action listener from the |
| public void | restart()
Restarts the |
| public void | setActionCommand(String
the action command command)Sets the string that will be delivered as the action command
in |
| public void | setCoalesce(boolean
specify flag)false to turn off coalescingSets whether the |
| public void | setDelay(int
the delay in milliseconds delay)Sets the |
| public void | setInitialDelay(int
the initial delay, in milliseconds initialDelay)Sets the |
| public static void | |
| public void | setRepeats(boolean
specify flag)false to make the timer
stop after sending its first action eventIf |
| public void | |
| public void | |
| private TimerQueue |
| actionCommand | back to summary |
|---|---|
| private volatile String actionCommand | |
| coalesce | back to summary |
|---|---|
| private volatile boolean coalesce | |
| delay | back to summary |
|---|---|
| private volatile int delay | |
| delayedTimer | back to summary |
|---|---|
| pack-priv transient TimerQueue. | |
| doPostEvent | back to summary |
|---|---|
| private final transient Runnable doPostEvent | |
| initialDelay | back to summary |
|---|---|
| private volatile int initialDelay | |
| listenerList | back to summary |
|---|---|
| protected EventListenerList listenerList The collection of registered listeners | |
| lock | back to summary |
|---|---|
| private final transient Lock lock | |
| logTimers | back to summary |
|---|---|
| private static volatile boolean logTimers | |
| notify | back to summary |
|---|---|
| private final transient AtomicBoolean notify | |
| repeats | back to summary |
|---|---|
| private volatile boolean repeats | |
| Timer | back to summary |
|---|---|
| public Timer(int delay, ActionListener listener) Creates a
| |
| addActionListener | back to summary |
|---|---|
| public void addActionListener(ActionListener listener) Adds an action listener to the
| |
| cancelEvent | back to summary |
|---|---|
| pack-priv void cancelEvent() Resets the internal state to indicate this Timer shouldn't notify
any of its listeners. This does not stop a repeatable Timer from
firing again, use | |
| checkDelay | back to summary |
|---|---|
| private static void checkDelay(int delay, String message) | |
| fireActionPerformed | back to summary |
|---|---|
| protected void fireActionPerformed(ActionEvent e) Notifies all listeners that have registered interest for notification on this event type.
| |
| getActionCommand | back to summary |
|---|---|
| public String getActionCommand() Returns the string that will be delivered as the action command
in
| |
| getActionListeners | back to summary |
|---|---|
| public ActionListener[] getActionListeners() Returns an array of all the action listeners registered on this timer.
| |
| getDelay | back to summary |
|---|---|
| public int getDelay() Returns the delay, in milliseconds, between firings of action events.
| |
| getInitialDelay | back to summary |
|---|---|
| public int getInitialDelay() Returns the
| |
| getListeners | back to summary |
|---|---|
| public <T extends EventListener> T[] getListeners(Class<T> listenerType) Returns an array of all the objects currently registered as
You can specify the ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));If no such listeners exist, this method returns an empty array.
| |
| getLock | back to summary |
|---|---|
| pack-priv Lock getLock() | |
| getLogTimers | back to summary |
|---|---|
| public static boolean getLogTimers() Returns
| |
| isCoalesce | back to summary |
|---|---|
| public boolean isCoalesce() Returns
| |
| isRepeats | back to summary |
|---|---|
| public boolean isRepeats() Returns
| |
| isRunning | back to summary |
|---|---|
| public boolean isRunning() Returns
| |
| post | back to summary |
|---|---|
| pack-priv void post() | |
| readObject | back to summary |
|---|---|
private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException
| |
| readResolve | back to summary |
|---|---|
private Object readResolve()
| |
| removeActionListener | back to summary |
|---|---|
| public void removeActionListener(ActionListener listener) Removes the specified action listener from the
| |
| restart | back to summary |
|---|---|
| public void restart() Restarts the | |
| setActionCommand | back to summary |
|---|---|
| public void setActionCommand(String command) Sets the string that will be delivered as the action command
in
| |
| setCoalesce | back to summary |
|---|---|
| public void setCoalesce(boolean flag) Sets whether the
| |
| setDelay | back to summary |
|---|---|
| public void setDelay(int delay) Sets the
| |
| setInitialDelay | back to summary |
|---|---|
| public void setInitialDelay(int initialDelay) Sets the
| |
| setLogTimers | back to summary |
|---|---|
| public static void setLogTimers(boolean flag) Enables or disables the timer log. When enabled, a message
is posted to
| |
| setRepeats | back to summary |
|---|---|
| public void setRepeats(boolean flag) If
| |
| start | back to summary |
|---|---|
| public void start() Starts the
| |
| stop | back to summary |
|---|---|
| public void stop() Stops the
| |
| timerQueue | back to summary |
|---|---|
| private TimerQueue timerQueue() Returns the timer queue. | |
Timer#post
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv Timer | |
| public void |
| DoPostEvent | back to summary |
|---|---|
| pack-priv DoPostEvent() | |
| getTimer | back to summary |
|---|---|
| pack-priv Timer getTimer() | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation. | |