I have the following code in a JPanel class which is added to a another class (JFrame). What I'm trying to implement is some sort of a stopwatch program.
startBtn.addActionListener(new startListener());
class startListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Timer time = new Timer();
time.scheduleAtFixedRate(new Stopwatch(), 1000, 1000);
}
}
This is another class which basically the task.
public class Stopwatch extends TimerTask {
private final double start = System.currentTimeMillis();
public void run() {
double curr = System.currentTimeMillis();
System.out.println((curr - start) / 1000);
}
}
The timer works fine and this is definitely far from complete but I'm not sure how to code the stop button which should stop the timer. Any advice on this? BTW I'm using java.util.timer
EDIT: I want to be able to start it again after stopping it (without the timer being reset)
If
javax.swing.Timer
is an acceptable alternative, as @Ash suggests, here is an example.I changed the trashgod's source.
update:
You can either cancel the entire
Timer
by callingTimer.cancel()
, or you can cancel individual tasks by callingTimerTask.cancel()
.Either way, you'll need to keep a reference to the timer and/or task instance when you create it, so that your stop routine can call the appropriate cancel method.
Update:
So you effectively want to be able to pause the timer? I don't think this is supported by the standard interface in
java.util.Timer
. You could do this by adding apause()
method (or similar) to your custom task, recording the elapsed time up to that point, and restarting the counting when the start button is clicked again. Note that using this technique, you wouldn't stop the timer task itself until you're finished with it completely. It still runs in the background, but you only do anything with it when the stopwatch has been started and is "running" (ie. some kind of flag to indicate the stopwatch's state).A couple of notes:
java.util.Timer
runs on a non-EDT thread, so if you're interacting with member variables in both the timer and in Swing action events, you'll need to appropriately handle the implications of multiple threads. It might be worth investigatingjavax.swing.Timer
, which will fire events on the EDT.Also, if you want a super-duper accurate stopwatch, you might consider using
System.nanoTime()
rather thancurrentTimeMillis()
.