| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
int DELAY = …;
ActionListener listener = …;
Timer t = new Timer(DELAY, listener);
Based on 36 examples
public class Timer extends Object implements Serializable
Fires one or more {@code ActionEvent}s 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();
{@code Timers} are constructed by specifying both a delay parameter
and an {@code 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 Timer
s perform their waiting
using a single, shared thread
(created by the first Timer
object that executes),
the action event handlers for Timer
s
execute on another thread -- the event-dispatching thread.
This means that the action handlers for Timer
s
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 {@link java.beans.XMLEncoder}.
Field Summary | |
---|---|
protected EventListenerList |
listenerList
|
Constructor Summary | |
---|---|
Timer(int delay, ActionListener listener) Creates a and initializes both the initial delay and between-event delay to milliseconds. |
Method Summary | |
---|---|
void |
addActionListener(ActionListener listener) Adds an action listener to the Timer .
|
protected void |
Notifies all listeners that have registered interest for notification on this event type. |
String |
Returns the string that will be delivered as the action command in ActionEvent s fired by this timer.
|
ActionListener[] |
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 |
Returns the Timer 's initial delay.
|
EventListener[] |
getListeners(Class listenerType) Returns an array of all the objects currently registered as FooListener s
upon this Timer .
|
static boolean |
Returns true if logging is enabled.
|
boolean |
Returns true if the Timer coalesces
multiple pending action events.
|
boolean |
Returns true (the default)
if the Timer will send
an action event
to its listeners multiple times.
|
boolean |
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 |
setActionCommand(String command) Sets the string that will be delivered as the action command in ActionEvent s fired by this timer.
|
void |
setCoalesce(boolean flag) Sets whether the Timer coalesces multiple pending
ActionEvent firings.
|
void |
setDelay(int delay) Sets the Timer 's between-event delay, the number of milliseconds
between successive action events.
|
void |
setInitialDelay(int initialDelay) Sets the Timer 's initial delay, the time
in milliseconds to wait after the timer is started
before firing the first event.
|
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)
listener
is not null
,
it's registered as an action listener on the timer.
delay
- milliseconds for the initial and between-event delaylistener
- an initial listener; can be null
Method Detail |
---|
public void addActionListener(ActionListener listener)
Timer
.
listener
- the listener to addprotected void fireActionPerformed(ActionEvent e)
e
- the action event to firepublic String getActionCommand()
ActionEvent
s fired by this timer. May be
null
, which is also the default.
public ActionListener[] getActionListeners()
ActionListener
s or an empty
array if no action listeners are currently registeredpublic int getDelay()
public int getInitialDelay()
Timer
's initial delay.
public EventListener[] getListeners(Class listenerType)
FooListener
s
upon this Timer
.
FooListener
s
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.
listenerType
- the type of listeners requested;
this parameter should specify an interface
that descends from java.util.EventListener
FooListener
s
on this timer,
or an empty array if no such
listeners have been addedpublic static boolean getLogTimers()
true
if logging is enabled.
true
if logging is enabled; otherwise, falsepublic boolean isCoalesce()
true
if the Timer
coalesces
multiple pending action events.
public boolean isRepeats()
true
(the default)
if the Timer
will send
an action event
to its listeners multiple times.
public boolean isRunning()
true
if the Timer
is running.
public void removeActionListener(ActionListener listener)
Timer
.
listener
- the listener to removepublic void restart()
Timer
,
canceling any pending firings and causing
it to fire with its initial delay.
public void setActionCommand(String command)
ActionEvent
s fired by this timer.
null
is an acceptable value.
command
- the action commandpublic void setCoalesce(boolean flag)
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.
Timer
s
coalesce events by default.
flag
- specify false
to turn off coalescingpublic void setDelay(int delay)
Timer
's between-event delay, the number of milliseconds
between successive action events. This does not affect the initial delay
property, which can be set by the {@code setInitialDelay} method.
delay
- the delay in millisecondspublic void setInitialDelay(int initialDelay)
Timer
's initial delay, the time
in milliseconds to wait after the timer is started
before firing the first event. Upon construction, this
is set to be the same as the between-event delay,
but then its value is independent and remains unaffected
by changes to the between-event delay.
initialDelay
- the initial delay, in millisecondspublic static void setLogTimers(boolean flag)
System.out
whenever the timer goes off.
flag
- true
to enable loggingpublic void setRepeats(boolean flag)
flag
is false
,
instructs the Timer
to send only one
action event to its listeners.
flag
- specify false
to make the timer
stop after sending its first action eventpublic void start()
Timer
,
causing it to start sending action events
to its listeners.
public void stop()
Timer
,
causing it to stop sending action events
to its listeners.
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |