I have the following code in a JPanel class that is being added to another class (JFrame). What I'm trying to implement is a kind of 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 that basically performs 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 it's definitely far from over, but I'm not sure how to encode the stop button that should stop the timer. Any advice on this? BTW I am using java.util.timer
EDIT: I want to be able to start it again after it stops (without a reset timer)
source
share