Timer: clock label : Timer « Swing JFC « Java






Timer: clock label

Timer: clock label
 

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class ClockTest extends JFrame {

  public ClockTest() {
    super("Timer Demo");
    setSize(300, 100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    ClockLabel clock = new ClockLabel();
    getContentPane().add(clock, BorderLayout.NORTH);
  }

  public static void main(String args[]) {
    ClockTest ct = new ClockTest();
    ct.setVisible(true);
  }
}
class ClockLabel extends JLabel implements ActionListener {

  public ClockLabel() {
    super("" + new Date());
    Timer t = new Timer(1000, this);
    t.start();
  }

  public void actionPerformed(ActionEvent ae) {
    setText((new Date()).toString());
  }
}

           
         
  








Related examples in the same category

1.Timer SampleTimer Sample
2.Timer with ProgressBar
3.Tick Tock with an Inner Class
4.Tick Tock with a Static Inner Class
5.Swing Timer Demo
6.Time Resolution
7.An applet that counts down from a specified time
8.Using swing TimerUsing swing Timer