Stop a stopwatch
- by James Morgan
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