Java: Example - Text Clock 2

This is the same as Example - Text Clock, but defines a reusable component for the clock. See javax.swing.Timer for an explanation of a simple timer class.

Main program / Applet

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
// File:   textclock/TextClock2.java
// Description: Application / Applet for a simple text clock.
// Author: Fred Swartz
// Date:   2005-02-15, 1999-05-01, 2001-11-02, 2002-11-07

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

///////////////////////////////////////////////////////////// TextClock2
public class TextClock2 extends JApplet {

    //====================================================== constructor
    public TextClock2() {
        add(new Clock());
    }

    //============================================================= main
    public static void main(String[] args) {
        JFrame window = new JFrame("Time of Day");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        window.setContentPane(new Clock());

        window.pack();
        window.setVisible(true);
        System.out.println(window.getContentPane().size());
    }
}

Clock component

This text clock component is created by subclassing JTextField. Whether this is better than making it a part of a JPanel is debatable, as it goes against the principle that one should Favor Composition over Inheritance.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
// File:   textclock/Clock.java
// Description: This implements a text clock by subclassing
//              JTextField, changing its appearance and
//              adding a timer to update itself.
//              Regarding subclassing JTextField
//              PROS: It's simple.
//                    All JTextField methods are available
//                        to the user, so they can easily
//                        customize it (fonts, colors, ...);
//              CONS: It can't become more complicated, eg,
//                        to add buttons, etc. because the
//                        users may already be using the
//                        JTextField methods.
// Author: Fred Swartz,
// Date:   15 Feb 2005
// Possible Enhancements: 
//         Appearance: center, leading zeros, uneditable, 
//         Function:   12/24 hour, alarm, timer, stop and start.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;

///////////////////////////////////////////////////////////// Clock
class Clock extends JTextField {
    javax.swing.Timer m_t;

    //================================================== constructor
    public Clock() {
        //... Set some attributes.
        setColumns(6);
        setFont(new Font("sansserif", Font.PLAIN, 48));

        //... Create a 1-second timer.
        m_t = new javax.swing.Timer(1000, new ClockTickAction());
        m_t.start();  // Start the timer
    }

    /////////////////////////////////////////// inner class listener
    private class ClockTickAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //... Get the current time.
            Calendar now = Calendar.getInstance();
            int h = now.get(Calendar.HOUR_OF_DAY);
            int m = now.get(Calendar.MINUTE);
            int s = now.get(Calendar.SECOND);
            setText("" + h + ":" + m + ":" + s);
        }
    }
}