001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package examples.clock;
043:
044: import java.util.Date;
045: import java.util.GregorianCalendar;
046: import java.util.Calendar;
047: import java.text.SimpleDateFormat;
048: import javax.swing.JOptionPane;
049:
050: public class ClockFrame extends javax.swing.JFrame {
051:
052: /** Initializes the Form */
053: public ClockFrame() {
054: initComponents();
055: pack();
056: }
057:
058: /** This method is called from within the constructor to
059: * initialize the form.
060: * WARNING: Do NOT modify this code. The content of this method is
061: * always regenerated by the FormEditor.
062: */
063: private void initComponents() {//GEN-BEGIN:initComponents
064: tmrSeconds = new org.netbeans.examples.lib.timerbean.Timer();
065: jlblCurrentTime = new javax.swing.JLabel();
066: jPanel1 = new javax.swing.JPanel();
067: jlblNewTime = new javax.swing.JLabel();
068: jtfNewTime = new javax.swing.JTextField();
069: jbtnNewTime = new javax.swing.JButton();
070: jPanel2 = new javax.swing.JPanel();
071: jlblNewFormat = new javax.swing.JLabel();
072: jtfNewTimeFormat = new javax.swing.JTextField();
073: jbtnNewTimeFormat = new javax.swing.JButton();
074:
075: tmrSeconds
076: .addTimerListener(new org.netbeans.examples.lib.timerbean.TimerListener() {
077: public void onTime(java.awt.event.ActionEvent evt) {
078: tmrSecondsOnTime(evt);
079: }
080: });
081:
082: addWindowListener(new java.awt.event.WindowAdapter() {
083: public void windowClosing(java.awt.event.WindowEvent evt) {
084: exitForm(evt);
085: }
086: });
087:
088: jlblCurrentTime.setText("00:00:00");
089: jlblCurrentTime
090: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
091: jlblCurrentTime.setFont(new java.awt.Font("Dialog", 1, 36));
092: getContentPane().add(jlblCurrentTime,
093: java.awt.BorderLayout.CENTER);
094:
095: jPanel1.setLayout(new java.awt.GridLayout(1, 0));
096:
097: jlblNewTime.setText("New Time");
098: jlblNewTime
099: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
100: jPanel1.add(jlblNewTime);
101:
102: jtfNewTime.setText("00:00:00");
103: jPanel1.add(jtfNewTime);
104:
105: jbtnNewTime.setText("Set New Time");
106: jbtnNewTime
107: .addActionListener(new java.awt.event.ActionListener() {
108: public void actionPerformed(
109: java.awt.event.ActionEvent evt) {
110: jbtnSetNewTimeClicked(evt);
111: }
112: });
113:
114: jPanel1.add(jbtnNewTime);
115:
116: getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
117:
118: jPanel2.setLayout(new java.awt.GridLayout(1, 0));
119:
120: jlblNewFormat.setText("Time Format");
121: jlblNewFormat
122: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
123: jPanel2.add(jlblNewFormat);
124:
125: jtfNewTimeFormat.setText("hh:mm:ss");
126: jPanel2.add(jtfNewTimeFormat);
127:
128: jbtnNewTimeFormat.setText("Set New Time Format");
129: jbtnNewTimeFormat
130: .addActionListener(new java.awt.event.ActionListener() {
131: public void actionPerformed(
132: java.awt.event.ActionEvent evt) {
133: jbtnNewTimeFormatActionClicked(evt);
134: }
135: });
136:
137: jPanel2.add(jbtnNewTimeFormat);
138:
139: getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
140:
141: }//GEN-END:initComponents
142:
143: private void jbtnNewTimeFormatActionClicked(
144: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNewTimeFormatActionClicked
145: String timeFormat = jtfNewTimeFormat.getText();
146: formatter = new SimpleDateFormat(timeFormat);
147: }//GEN-LAST:event_jbtnNewTimeFormatActionClicked
148:
149: private void jbtnSetNewTimeClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSetNewTimeClicked
150: try {
151: String timeStr = jtfNewTime.getText();
152: gCal.setTime(formatter.parse(timeStr));
153: } catch (java.text.ParseException e) {
154: JOptionPane.showMessageDialog(this , "Invalid date format",
155: "I don't understand your date format.",
156: JOptionPane.ERROR_MESSAGE);
157: }
158: }//GEN-LAST:event_jbtnSetNewTimeClicked
159:
160: private void tmrSecondsOnTime(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tmrSecondsOnTime
161: gCal.add(Calendar.SECOND, 1);
162: String timeTxt = formatter.format(gCal.getTime());
163: if (jlblCurrentTime != null)
164: jlblCurrentTime.setText(timeTxt);
165: }//GEN-LAST:event_tmrSecondsOnTime
166:
167: /** Exit the Application */
168: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
169: System.exit(0);
170: }//GEN-LAST:event_exitForm
171:
172: // Variables declaration - do not modify//GEN-BEGIN:variables
173: private javax.swing.JButton jbtnNewTime;
174: private javax.swing.JButton jbtnNewTimeFormat;
175: private javax.swing.JTextField jtfNewTime;
176: private javax.swing.JLabel jlblNewTime;
177: private javax.swing.JLabel jlblNewFormat;
178: private javax.swing.JLabel jlblCurrentTime;
179: private org.netbeans.examples.lib.timerbean.Timer tmrSeconds;
180: private javax.swing.JTextField jtfNewTimeFormat;
181: private javax.swing.JPanel jPanel2;
182: private javax.swing.JPanel jPanel1;
183: // End of variables declaration//GEN-END:variables
184: private GregorianCalendar gCal = new GregorianCalendar();
185: private String timeFormat = "hh:mm:ss";
186: private SimpleDateFormat formatter = new SimpleDateFormat(
187: timeFormat);
188:
189: public static void main(java.lang.String[] args) {
190: new ClockFrame().show();
191: }
192:
193: }
|