Java tutorial
package dataform.components; /* * Copyright 2013 Georges Stephan * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import org.apache.commons.lang.time.*; /** * * A Stopwatch based on the apache commons lang * * @author Georges Stephan */ public class WorkTimer extends JMenuTextField { public final static int ONE_SECOND = 1000; private javax.swing.Timer timer; private StopWatch stopWatch; public WorkTimer() { try { init(); } catch (Exception ex) { ex.printStackTrace(); } } public void reset() { stopWatch.stop(); stopWatch = new StopWatch(); stopWatch.start(); } private void init() throws Exception { stopWatch = new StopWatch(); setEditable(false); setForeground(Color.RED); setHorizontalAlignment(JLabel.RIGHT); setMargin(new Insets(2, 2, 2, 4)); timer = new javax.swing.Timer(ONE_SECOND, new ActionListener() { public void actionPerformed(ActionEvent evt) { timer_ActionPerformed(evt); } }); stopWatch.start(); timer.start(); } private void timer_ActionPerformed(ActionEvent evt) { stopWatch.split(); setText(DateFormatUtils.formatUTC(stopWatch.getTime(), "HH:mm:ss", new Locale("EN", "GB"))); stopWatch.unsplit(); } }