The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
< Day Day Up > |
A Swing timer (an instance of javax.swing.Timer [81] ) fires one or more action events after a specified delay. Don't confuse it with the general-purpose timer facility that was added to the java.util package in release 1.3. [82] [81] javax.swing.Timer API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/Timer.html. [82] This section describes only Swing timers; you can find information on general-purpose timers in "Using the Timer and TimerTask Classes" in The Java Tutorial , available on the CD and online: http://java.sun.com/docs/books/tutorial/essential/threads/timer.html. In general, we recommend using Swing timers rather than general-purpose timers for GUI-related tasks because Swing timers all share the same, preexisting timer thread and the GUI- related task automatically executes on the event-dispatching thread. However, you might use a general-purpose timer if you don't plan on touching the GUI from the timer or you need to perform lengthy processing. You can use Swing timers in two ways:
Swing timers are very easy to use. When you create the timer, you specify an action listener to be notified when the timer "goes off." The actionPerformed method in this listener should contain the code for whatever task you need carried out. You also specify the number of milliseconds between timer firings. If you want the timer to go off only once, you can invoke setRepeats(false) on it. To start the timer, call its start method. To suspend it, call stop . The Swing timer's task is performed in the event-dispatching thread. This means that it can safely manipulate components , but it also means that the task should execute quickly. If it might take a while to execute, consider using a SwingWorker instead of or in addition to the timer. See How to Use Threads (page 632) for instructions on using the SwingWorker class and information on using Swing components in multi-threaded programs. Figure 40 is an example of using a timer to periodically update a component that displays progress toward a goal. Figure 40. A screenshot of an application that uses a timer and a progress bar to display the progress of a long-running task.
Try This:
Here's the code from ProgressBarDemo.java that creates a timer set to go off every second. Each time the timer goes off it receives an action event. The action listener contains the code that implements the timer's task. In this case, the handler for the action event has to find out how close a thread is to completing its work and then update the progress bar accordingly . If the thread has completed its work, the action listener also stops the timer and updates the GUI to show that the thread is finished. public final static int ONE_SECOND = 1000; ... timer = new Timer(ONE_SECOND, new ActionListener() { public void actionPerformed(ActionEvent evt) { //...Update the progress bar... if ( /* thread is done */ ) { timer.stop(); //...Update the GUI... } } }); When the user presses the Start button, the program starts the timer: timer.start(); The Timer API
Tables 23 and 24 list the commonly used javax.swing.Timer constructors and methods . You can also refer to the API documentation for javax.swing.Timer at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/Timer.html. Table 23. Creating and Initializing the Timer
Table 24. Running the Timer
Examples That Use Timers
This table shows examples that use javax.swing.Timer and where those examples are described.
|
< Day Day Up > |