javax.swing
public class Timer extends Object implements Serializable
java.util.Timer
just for
firing ActionEvent
s. All Timers share one (daemon)
Thread (or java.util.Timer). All events are fired from the event
queue.Modifier and Type | Field and Description |
---|---|
protected EventListenerList |
listenerList
A field to store all listeners who are listening to this timer.
|
Constructor and Description |
---|
Timer(int d,
ActionListener listener)
Creates a new Timer object.
|
Modifier and Type | Method and Description |
---|---|
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> |
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.
|
protected EventListenerList listenerList
public Timer(int d, ActionListener listener)
d
- the default value for both initial and between event delay, in
milliseconds.listener
- the first action listener, can be null
.public ActionListener[] getActionListeners()
public void setCoalesce(boolean c)
c
- true
(default) to enable the event coalescing,
false
otherwisepublic boolean isCoalesce()
true
if the coalescing is enabled,
false
otherwisepublic <T extends EventListener> T[] getListeners(Class<T> listenerType)
listenerType
- the listener type (for example, ActionListener.class)public static void setLogTimers(boolean lt)
true
, the
timer prints a message to System.out
when firing each
action event.lt
- true
if logging is enabled, false
(default value) otherwisepublic static boolean getLogTimers()
true
if the timer is printing a message to
System.out
when firing each action eventpublic void setDelay(int d)
d
- The time gap between the subsequent events, in millisecondsIllegalArgumentException
- if d
is less than zero.public int getDelay()
public void setInitialDelay(int i)
start()
method. If the initial delay has not been
set, it is assumed having the same value as the delay between the
subsequent events.i
- the initial delay, in millisecondsIllegalArgumentException
- if i
is less than zero.public int getInitialDelay()
start()
method. If the initial delay has not been
set, returns the same value as getDelay()
.public void setRepeats(boolean r)
r
- true
(default value) to fire repetetive events.
false
to fire
only one event after the initial delaypublic boolean isRepeats()
true
if the timer fires repetetive events,
false
if it fires
only one event after the initial delaypublic boolean isRunning()
true
if the timer has been started and is firing
the action events as scheduled. false
if the timer is inactive.public void addActionListener(ActionListener listener)
listener
- the action listener to addpublic void removeActionListener(ActionListener listener)
listener
- the action listener to removepublic void restart()
public void start()
public void stop()
protected void fireActionPerformed(ActionEvent event)
event
- the event to fire