KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ixenon > free > swing > DigitalClock


1 /* $Id$
2  *
3  * Copyright (c) 1998 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  */

15
16 // DigitalClock.java
17

18 // Description:
19
// Installer frame for free installer application
20
//
21
// Author:
22
// Peter Pilgrim
23
// Mon Jan 11 23:50:46 GMT 1999
24
//
25
// RCS HEADER
26
//
27
// $Author$
28
// $Date$
29
// $Source$
30
// $Revision$ $State$ $Locker$
31
//
32
// History
33
// ================================================================================
34
// $Log$
35

36 package ixenon.free.swing;
37
38 import java.awt.*;
39 import java.awt.event.*;
40 import java.util.Date JavaDoc;
41 import java.text.*; // for `SimpleDataFormat' class
42
import javax.swing.*; // for JLabel
43
import javax.swing.event.*;
44
45 // import ixenon.free.util.*; // for ApplicationResources
46

47
48 /**
49  * A pseudo Swing component which displays a DigitalClock,
50  * which displays the current time the format `HH:mm:ss AA'
51  * where HH is the hour, MM is the minute, SS is the
52  * seconds and AA is the AM or PM depending on if the
53  * 24hr mode or 12 hour mode is set.
54  *
55  * @author Peter Pilgrim Mon Jan 25 22:19:07 GMT 1999
56  * @version $Id$
57  */

58 public class DigitalClock extends JLabel
59 implements ActionListener
60 {
61     private final static String JavaDoc MODE12 = "hh:mm:ss aa";
62     private final static String JavaDoc MODE24 = "HH:mm:ss --";
63     
64     private boolean mode24Hours; // true if twenty four hours
65
private Timer timer; // The internal timer
66
private SimpleDateFormat stamper;
67
68     /**
69      * Constructs the digital clock
70      */

71     public DigitalClock()
72     {
73     timer = new Timer( 1000, this );
74     timer.setInitialDelay( 100 );
75     timer.setCoalesce(true);
76     stamper = new SimpleDateFormat( MODE24 );
77     mode24Hours = true;
78     setText( "--:--:-- ---" );
79     // stop();
80
}
81
82     /** Sets the 24 or 12 hour mode */
83     public void setMode24Hours( boolean mode )
84     {
85     mode24Hours = mode;
86     if (mode24Hours)
87         stamper = new SimpleDateFormat( MODE24 );
88     else
89         stamper = new SimpleDateFormat( MODE12 );
90     if ( timer.isRunning() )
91         updateClock();
92     }
93     
94     /** if true when digital clock is in 24 hour mode */
95     public boolean isMode24Hours()
96     {
97     return (mode24Hours);
98     }
99     
100     /** Starts the digital clock */
101     public boolean isRunnning()
102     {
103     return (timer.isRunning() );
104     }
105
106     /** Starts the digital clock */
107     public void start()
108     {
109     if ( !timer.isRunning() )
110         timer.start();
111     }
112
113     /** Stops the digital clock */
114     public void stop()
115     {
116     if ( timer.isRunning() )
117         timer.stop();
118     }
119
120     /** Resets the digital clock */
121     public void reset()
122     {
123     if ( timer.isRunning() ) {
124         timer.stop();
125         setText( "--:--:-- ---" );
126     }
127     }
128
129     /** Update the digital clock */
130     protected void updateClock()
131     {
132     Date JavaDoc now = new Date JavaDoc();
133     String JavaDoc dtstr = stamper.format( now );
134     setText(dtstr);
135     }
136     
137     public void actionPerformed( ActionEvent e )
138     {
139     Object JavaDoc src = e.getSource();
140     if ( timer == src ) {
141         updateClock();
142     }
143     }
144     
145 }
146
147 // fini
148
Popular Tags