KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > clock > ClockFrame


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package examples.clock;
21
22 import java.util.Date JavaDoc;
23 import java.util.GregorianCalendar JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import javax.swing.JOptionPane JavaDoc;
27
28 public class ClockFrame extends javax.swing.JFrame JavaDoc {
29
30     /** Initializes the Form */
31     public ClockFrame() {
32         initComponents ();
33         pack ();
34     }
35
36     /** This method is called from within the constructor to
37      * initialize the form.
38      * WARNING: Do NOT modify this code. The content of this method is
39      * always regenerated by the FormEditor.
40      */

41     private void initComponents() {//GEN-BEGIN:initComponents
42
tmrSeconds = new org.netbeans.examples.lib.timerbean.Timer();
43         jlblCurrentTime = new javax.swing.JLabel JavaDoc();
44         jPanel1 = new javax.swing.JPanel JavaDoc();
45         jlblNewTime = new javax.swing.JLabel JavaDoc();
46         jtfNewTime = new javax.swing.JTextField JavaDoc();
47         jbtnNewTime = new javax.swing.JButton JavaDoc();
48         jPanel2 = new javax.swing.JPanel JavaDoc();
49         jlblNewFormat = new javax.swing.JLabel JavaDoc();
50         jtfNewTimeFormat = new javax.swing.JTextField JavaDoc();
51         jbtnNewTimeFormat = new javax.swing.JButton JavaDoc();
52
53         tmrSeconds.addTimerListener(new org.netbeans.examples.lib.timerbean.TimerListener() {
54             public void onTime(java.awt.event.ActionEvent JavaDoc evt) {
55                 tmrSecondsOnTime(evt);
56             }
57         });
58
59         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
60             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
61                 exitForm(evt);
62             }
63         });
64
65         jlblCurrentTime.setText("00:00:00");
66         jlblCurrentTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
67         jlblCurrentTime.setFont(new java.awt.Font JavaDoc("Dialog", 1, 36));
68         getContentPane().add(jlblCurrentTime, java.awt.BorderLayout.CENTER);
69
70         jPanel1.setLayout(new java.awt.GridLayout JavaDoc(1, 0));
71
72         jlblNewTime.setText("New Time");
73         jlblNewTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
74         jPanel1.add(jlblNewTime);
75
76         jtfNewTime.setText("00:00:00");
77         jPanel1.add(jtfNewTime);
78
79         jbtnNewTime.setText("Set New Time");
80         jbtnNewTime.addActionListener(new java.awt.event.ActionListener JavaDoc() {
81             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
82                 jbtnSetNewTimeClicked(evt);
83             }
84         });
85
86         jPanel1.add(jbtnNewTime);
87
88         getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
89
90         jPanel2.setLayout(new java.awt.GridLayout JavaDoc(1, 0));
91
92         jlblNewFormat.setText("Time Format");
93         jlblNewFormat.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
94         jPanel2.add(jlblNewFormat);
95
96         jtfNewTimeFormat.setText("hh:mm:ss");
97         jPanel2.add(jtfNewTimeFormat);
98
99         jbtnNewTimeFormat.setText("Set New Time Format");
100         jbtnNewTimeFormat.addActionListener(new java.awt.event.ActionListener JavaDoc() {
101             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
102                 jbtnNewTimeFormatActionClicked(evt);
103             }
104         });
105
106         jPanel2.add(jbtnNewTimeFormat);
107
108         getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
109
110     }//GEN-END:initComponents
111

112     private void jbtnNewTimeFormatActionClicked (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jbtnNewTimeFormatActionClicked
113
String JavaDoc timeFormat = jtfNewTimeFormat.getText();
114         formatter = new SimpleDateFormat JavaDoc(timeFormat);
115     }//GEN-LAST:event_jbtnNewTimeFormatActionClicked
116

117     private void jbtnSetNewTimeClicked (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jbtnSetNewTimeClicked
118
try {
119             String JavaDoc timeStr = jtfNewTime.getText();
120             gCal.setTime(formatter.parse(timeStr));
121         } catch (java.text.ParseException JavaDoc e) {
122             JOptionPane.showMessageDialog(this, "Invalid date format", "I don't understand your date format.", JOptionPane.ERROR_MESSAGE);
123         }
124     }//GEN-LAST:event_jbtnSetNewTimeClicked
125

126     private void tmrSecondsOnTime (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_tmrSecondsOnTime
127
gCal.add(Calendar.SECOND,1);
128         String JavaDoc timeTxt = formatter.format(gCal.getTime());
129         if (jlblCurrentTime != null)
130             jlblCurrentTime.setText(timeTxt);
131     }//GEN-LAST:event_tmrSecondsOnTime
132

133     /** Exit the Application */
134     private void exitForm(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_exitForm
135
System.exit (0);
136     }//GEN-LAST:event_exitForm
137

138
139     // Variables declaration - do not modify//GEN-BEGIN:variables
140
private javax.swing.JButton JavaDoc jbtnNewTime;
141     private javax.swing.JButton JavaDoc jbtnNewTimeFormat;
142     private javax.swing.JTextField JavaDoc jtfNewTime;
143     private javax.swing.JLabel JavaDoc jlblNewTime;
144     private javax.swing.JLabel JavaDoc jlblNewFormat;
145     private javax.swing.JLabel JavaDoc jlblCurrentTime;
146     private org.netbeans.examples.lib.timerbean.Timer tmrSeconds;
147     private javax.swing.JTextField JavaDoc jtfNewTimeFormat;
148     private javax.swing.JPanel JavaDoc jPanel2;
149     private javax.swing.JPanel JavaDoc jPanel1;
150     // End of variables declaration//GEN-END:variables
151
private GregorianCalendar JavaDoc gCal = new GregorianCalendar JavaDoc();
152     private String JavaDoc timeFormat = "hh:mm:ss";
153     private SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(timeFormat);
154
155
156     public static void main(java.lang.String JavaDoc[] args) {
157         new ClockFrame ().show ();
158     }
159
160 }
161
Popular Tags