Hey.. I'm having problems with stopping this timer from outside the
'runTimer()' class.. i have no problems calling 'cancel()' from inside
the run() method but I want to be able to stop the Timer when I press
a button (stopTimer()).
With the code I've got here, i get an error saying 'void cannot be
dereferenced' ... has anyone got any ideas as to what I'm doing wrong
or how i could stop the timer by pressing the button? Any help would
be appreciated.. i'm fairly new to java so if you can keep it in
relative laymens terms thanks! :)
public void runTimer(){
int delay = 0; //
int period = 3000; //
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
//code does stuff here
}
}, delay, period);
}
//when stopTimer button is pressed.. timer should stop
public void stopTimer() {
runTimer().timer.cancel();
}