I'm using JavaFX 8 to develop a little game as a side project and I want to use the Timer
class from the Java.util
package.
The thing is, whenever I schedule the Timer
to do something, I don't know how to stop it, and it keeps running in the background even when I've already closed the window.
I eventually made a method called handleShutDown()
that's called whenever the Stage
is set on hidden, using the code below.
stage.setOnHidden(windowEvent -> controller.handleShutDown());
I also tried a couple of different ways to cancel the Timer
in the handleShutDown()
method. I tried calling the Timer's cancel()
method, the Timer's purge()
method, setting the Timer to null
, and even replacing the Timer with a new one (timer = new Timer()
).
public void handleShutDown() {
// timer.cancel();
// timer.purge();
// timer = new Timer();
timer = null;
Platform.exit();
}
I'm not sure what to try next...
Here's how I know the application is still running, since the red box is still there even after I closed the window, which should not happen. And everything was fine until I started using timers. Or maybe I shouldn't use a Timer?
Thanks in advance.