01: import java.awt.*;
02: import java.awt.event.*;
03: import java.util.*;
04: import javax.swing.*;
05: import javax.swing.Timer;
06: 
07: /**
08:    This program shows a clock that is updated once per second.
09: */
10: public class TimerTester
11: {
12:    public static void main(String[] args)
13:    {
14:       JFrame frame = new JFrame();
15: 
16:       final int FIELD_WIDTH = 20;
17:       final JTextField textField = new JTextField(FIELD_WIDTH);
18: 
19:       frame.setLayout(new FlowLayout());
20:       frame.add(textField);
21: 
22:       ActionListener listener = new
23:          ActionListener()
24:          {
25:             public void actionPerformed(ActionEvent event)
26:             {
27:                Date now = new Date();
28:                textField.setText(now.toString());
29:             }
30:          };
31:       final int DELAY = 1000;
32:          // Milliseconds between timer ticks
33:       Timer t = new Timer(DELAY, listener);
34:       t.start();
35: 
36:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37:       frame.pack();
38:       frame.setVisible(true);
39:    }
40: }