Stop a stopwatch

Posted by James Morgan on Stack Overflow See other posts from Stack Overflow or by James Morgan
Published on 2010-04-05T00:06:07Z Indexed on 2010/04/05 0:13 UTC
Read the original article Hit count: 412

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about timer