Using Other Swing Features |
TheTimer
class fires one or moreActionEvent
s after a specified delay. Timers are useful in the following situations:Let's look at an example of situation #2. Here's a picture of a small demo application that uses a
- Doing something after a delay. For example, many Swing components, such as buttons, use a timer to determine when to display a tool tip.
- Showing periodic progress. The first example that follows,
ProgressBarDemo
, does this.- Performing animation. See Creating an Animation Loop with Timer for an example and discussion.
Timer
and a progress bar to display the progress of a long-running task.Once the task has begun, the timer causes the progress bar to update every second until the task completes. Here's the code from
Try this:
- Compile and run the application. The main source file is
ProgressBarDemo.java
.
See Getting Started with Swing if you need help.- Push the Start button. Watch the progress bar as the task makes progress.
ProgressBarDemo.java
that creates the timer and, when the user presses the Start button, starts it:Below is the code that implements the action listener that is notified each time the timer goes off:timer = new Timer(ONE_SECOND, new TimerListener()); . . . timer.start();The bold line of code stops the timer when the task completes.class TimerListener implements ActionListener { public void actionPerformed(ActionEvent evt) { progressBar.setValue(task.getCurrent()); if (task.done()) { Toolkit.getDefaultToolkit().beep(); timer.stop(); startButton.setEnabled(true); } } }
Note: TheactionPerformed
method defined in theTimer
's action listener is invoked in the event-dispatching thread. That means that you never have to use theinvokeLater
method in it. For more information about using Swing components and threads in the same program, refer to Threads and Swing.
The Timer API
The following tables list the commonly usedTimer
constructors and methods. The API for using timers falls into three categories:
Fine Tuning the Timer's Operation Method or Constructor Purpose Timer(int, ActionListener)
Create a timer set up with a delay and a listener. This is Timer
's only constructor.void setDelay(int)
int getDelay()Set or get the delay between firings. void setInitialDelay(int)
int getInitialDelay()Set or get the delay for the initial firing. If you don't set the initial delay, then it's the same as the delay between firings. void setRepeats(boolean)
boolean isRepeats()Set or get whether the timer repeats. [PENDING: Default value is true?] void setCoalesce(boolean)
boolean isCoalesce()Set or get whether the timer coalesces multiple, pending firings into a single firing.
Running the Timer Method Purpose void start()
void restart()Turn the timer on. restart
cancels any pending firings.void stop()
Turn the timer off. boolean isRunning()
Get whether the timer is running.
Listening to the Timer Fire Method Purpose void addActionListener(ActionListener)
void removeActionListener(ActionListener)Add or remove an action listener. Examples that Use Timer
This table shows the examples that useTimer
and where those examples are described.
Example Where Described ProgressBarDemo.java This section and How to Monitor Progress AnimatorApplicationTimer.java This section. SliderDemo.java How to Use Sliders
Using Other Swing Features |