title: How to prevent a Timer from running in the background?
body: <p>I'm using JavaFX 8 to develop a little game as a side project and I want to use the <code>Timer</code> class from the <code>Java.util</code> package.
  <br><br>
  The thing is, whenever I schedule the <code>Timer</code> 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.
  <br><br>
  I eventually made a method called <code>handleShutDown()</code> that's called whenever the <code>Stage</code> is set on hidden, using the code below.</p>
  <pre><code>stage.setOnHidden(windowEvent -&gt; controller.handleShutDown());
  </code></pre>
  <p>I also tried a couple of different ways to cancel the <code>Timer</code> in the <code>handleShutDown()</code> method. I tried calling the Timer's <code>cancel()</code> method, the Timer's <code>purge()</code> method, setting the Timer to <code>null</code>, and even replacing the Timer with a new one (<code>timer = new Timer()</code>).</p>
  <pre><code>public void handleShutDown() {
  //    timer.cancel();
  //    timer.purge();
  //    timer = new Timer();
      timer = null;
      Platform.exit();
  }
  </code></pre>
  <p>I'm not sure what to try next...
  <br><br>
  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?
  <br><br>
  <a href="https://i.stack.imgur.com/thiTO.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/thiTO.jpg" alt="Here's how I know the application is still running" /></a></p>
  <p>Thanks in advance.</p>