package edu.pdx.cs410G.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.*;

/**
 * This program demonstrates a Swing {@link Timer} that periodically
 * updates a {@link JTextField} with the current time.
 */
public class TimerExample extends JPanel {

  public TimerExample() {
    this.setLayout(new BorderLayout());
    final JTextField text = new JTextField(20);
    text.setEditable(false);
    this.add(BorderLayout.CENTER, text);
    
    ActionListener listener = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  text.setText(new Date().toString());
	}
      };
    Timer timer = new Timer(1000, listener);
    timer.setInitialDelay(0);
    timer.start();
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("A Timer");
    JPanel panel = new TimerExample();
    frame.getContentPane().add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }

}

package edu.pdx.cs410G.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.*;

/**
 * This program demonstrates a Swing {@link Timer} that periodically
 * updates a {@link JTextField} with the current time.
 */
public class TimerExample extends JPanel {

  private final Timer timer;

  public TimerExample() {
    this.setLayout(new BorderLayout());
    final JTextField text = new JTextField(20);
    text.setEditable(false);
    this.add(BorderLayout.CENTER, text);
    
    ActionListener listener = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  text.setText(new Date().toString());
	}
      };
    this.timer = new Timer(1000, listener);
    this.timer.setInitialDelay(0);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("A Timer");
    TimerExample example = new TimerExample();
    frame.getContentPane().add(example);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    // Start timer after GUI is visible
    example.timer.start();
  }

}

Make sure to start the timer after you display the GUI. Otherwise you'll get a blocked thread.
Make sure to start the timer after you display the GUI.  Otherwise
you'll get a blocked thread.

