tags:

views:

732

answers:

2

I have tried to reset the timer based on the current time after clicking a button, but it doesnt work. Help :-(

private long startTime  = System.currentTimeMillis();
Timer timer  = new Timer(1000, this);
timer.start();

timer.stop();
long endTime    = System.currentTimeMillis();
long timeInMilliseconds = (endTime - startTime);

timer.reset();
+2  A: 

My magic crystal ball says you are using a javax.swing.Timer and that there is no reset() method, it is called restart().

But then it could be wrong, it would be nice if you were a bit more explicit about what you are doing ...

starblue
Actually what I need was to start the timer again to the current system time. But it solved now...I just need to place the startTime = System.currentTimeMillis() again in ActionListener and not in main class.
Jessy
A: 

The solution for my program. Thanks everyone.

   public class mainClass {
        private long startTime  = System.currentTimeMillis();
        Timer timer  = new Timer(1000, this);
        .....
    }

    public mainClass {
        timer.start();
    }

    //Everytime the button stop clicked, the time will stop and reset to the most current time of the system
    public actionPerformed () {
        timer.stop();
        long endTime    = System.currentTimeMillis();
        long timeInMilliseconds = (endTime - startTime);

        **startTime  = System.currentTimeMillis();** ACCEPTED
    }
Jessy
For future reference, all Java classes (in your case, "mainClass") should start with a capital letters and you probably shouldn't use the word "class" in then name of a class.
basszero
Thanks for the suggestion.
Jessy