Swing Timers : Swing Timer « Swing « Java Tutorial





  1. javax.swing.Timer can only be used in Swing applications.
  2. javax.swing.Timer is a more appropriate choice over java.util.Timer for Swing applications.
  3. javax.swing.Timer handles thread sharing.
  4. You implement the java.awt.event.ActionListener interface and write your task code in its actionPerformed method.
  5. Ro cancel a task, you use the javax.swing.Timer class's stop method.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class MainClass {

  public static void main(String[] args) {
    Timer timer = new Timer(1000, new MyTimerActionListener());

    timer.start();
    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
    }
    timer.stop();
  }

}

class MyTimerActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {

    System.out.println("asdf");

  }
}










14.114.Swing Timer
14.114.1.Swing Timers
14.114.2.Timer Properties: coalesce
14.114.3.Timer Class
14.114.4.To turn on log messages
14.114.5.Timer based animation
14.114.6.A rotating and scaling rectangle.
14.114.7.Font size animation
14.114.8.Fade out an image: image gradually get more transparent until it is completely invisible.
14.114.9.Swing Timer actionSwing Timer action