Java Notes


A javax.swing.Timer object calls an action listener at regular intervals or only once. For example, it can be used to show frames of an animation many times per second, repaint a clock every second, or check a server every hour.

Java 2 added another class by the same name, but in the java.util package, java.util.Timer, which is better for large timing tasks. It also has more features (eg, it can start at a given time, use either a fixed delay or fixed rate), but javax.swing.Timer is easier to use for simple animations. It is also advisable to use java.util.Timer for headless programs, those without a GUI. To prevent ambiguous references, always write the fully qualified name.

To Use a Timer

  1. import java.awt.event.*; // for ActionListener and ActionEvent
  2. Create a Timer object, giving a time interval in milliseconds, and an action listener. This would usually be done once in a constructor. The prototype usage is:
        javax.swing.Timer yourTimer = new javax.swing.Timer(int milliseconds, ActionListener doIt);
    The following example creates a Timer that calls an anonymous action listener to repaint a panel every second (1000 milliseconds).
    javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
              public void actionPerformed(ActionEvent e) {
  3. Start the timer by calling the timer's start method. For example,

To Stop a Timer

Call the timer's stop method. Example,


See the discussion below to see when it's important to stop a timer in an applet.

Fixed interval versus fixed rate

javax.swing.Timer uses a fixed delay, which means that the time from the return from an action listener to the time of the next call is a fixed time. Therefore the interval between calls will be longer because of the time used by the action listener and overhead operations such as garbage collection. This is ok for many animations, but not where exact timing is required. The clock animation examples show this deficiency, as you may notice when the clock "skips" a second every so often to catch up to the real time. The java.util.Timer class provides a fixed rate option which would be a better way to run a clock, but I haven't made the time to update the examples.

Starting and Stopping Animation in an Applet

Because animation uses computer resources (CPU time and memory), the browse rmay ask the applet to stop what it is doing (by calling the applet's stop method) when the applet is no longer visible, eg, the browser moves to a different page or minimizes the page. It calls the applet's start method when it wants the applet to become active again. Applets usually don't need stop and start methods, but if your applet is using a Timer to display animation or is playing sound, provide these methods.

Writing start and stop methods for Applets

Starting and stopping a timer is very simple. For example, just add the following lines to your applet (assuming t is a Timer):

public void start() {t.start();}
public void stop()  {t.stop();}

Additional Timer methods

t.setRepeats(boolean flag);
t.setInitialDelay(int initialDelay);
if (t.isRunning()) ...

And others.

Examples of Timer