    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 TimerTest
    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:       Container contentPane = frame.getContentPane();
    20:       contentPane.setLayout(new FlowLayout());
    21:       contentPane.add(textField);
    22:
    23:       ActionListener listener = new
    24:          ActionListener()
    25:          {
    26:             public void actionPerformed(ActionEvent event)
    27:             {
    28:                Date now = new Date();
    29:                textField.setText(now.toString());
    30:             }
    31:          };
    32:       final int DELAY = 1000;
    33:          // milliseconds between timer ticks
    34:       Timer t = new Timer(DELAY, listener);
    35:       t.start();
    36:
    37:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    38:       frame.pack();
    39:       frame.show();
    40:    }
    41: }