Swing Timer action : Swing Timer « Swing « Java Tutorial






Swing Timer action
import java.awt.Color; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;

public class TimerChangeButtonBackground extends JFrame {
  boolean flag = false;

  JButton button = new JButton("Click to stop");

  Timer timer;

  public TimerChangeButtonBackground() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(button, "Center");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        timer.stop();
        button.setBackground(Color.red);
      }
    });
    timer = new Timer(1000, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button.setBackground(flag ? Color.green : Color.yellow);
        flag = !flag;
        repaint();
      }
    });
    timer.start();
    pack();
    setVisible(true);
  }

  public static void main(String arg[]) {
    new TimerChangeButtonBackground();
  }
}








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