title: JavaFX ToggleButton get in and out of an infinite loop
body: <p>I want to create a toggle button in JavaFX that will execute a loop, and will stop only when the user clicks the button again; while active, the loop will continuously change the text in the TextField...</p>
  
  <p>Here is the code for the ToggleButton:</p>
  
  <pre><code>toggleButton.setOnAction(event -&gt; {
      try {
  
          if(toggleButton.isSelected())
          {
              new Thread(
                      new Runnable() {
                          public void run() {
                              while(toggleButton.isSelected())
                              {
                                  try {
                                      Thread.sleep(200);
                                  } catch (InterruptedException e) {
                                      e.printStackTrace();
                                  }
  
                                  double rand = Math.random();
                                  textField.setText(rand);
                              }
                          }
                      }
                      ).start();
          }
  
          else if(!toggleButton.isSelected())
          {
              toggleButton.setSelected(false);
          }
  
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  });
  </code></pre>
  
  <p>Well, the above code <strong><em>does</em></strong> work, but... the problem is, that without slowing down the loop enough, (the Thread.sleep(200) thing), tons of errors will be printed in the console... But I need it to be as fast as possible...</p>
  
  <p>Here is an example of the output <strong>without</strong> using Thread.sleep(200):</p>
  
  <p>(Couldn't paste everything because it's way too long... but here's a link to the full output: <a href="http://pastebin.com/SZfugk3d" rel="nofollow">http://pastebin.com/SZfugk3d</a>)</p>
  
  <pre><code>[sts] -----------------------------------------------------
  [sts] Starting Gradle build for the following tasks: 
  [sts]      run
  [sts] -----------------------------------------------------
  :compileJava
  :compileRetrolambdaMain
  :processResources UP-TO-DATE
  :classes
  :compileAndroidJava SKIPPED
  :compileRetrolambdaAndroid SKIPPED
  :compileTestJava UP-TO-DATE
  :compileRetrolambdaTest SKIPPED
  :compileRetrolambda
  :compileDesktopJava UP-TO-DATE
  :processDesktopResources UP-TO-DATE
  :desktopClasses UP-TO-DATE
  :run
  Exception in thread "Thread-6" java.lang.NullPointerException
      at com.sun.javafx.text.PrismTextLayout.layout(PrismTextLayout.java:1209)
      at com.sun.javafx.text.PrismTextLayout.ensureLayout(PrismTextLayout.java:223)
      at com.sun.javafx.text.PrismTextLayout.getBounds(PrismTextLayout.java:246)
      at javafx.scene.text.Text.getLogicalBounds(Text.java:358)
      at javafx.scene.text.Text.getYRendering(Text.java:1069)
      at javafx.scene.text.Text.access$4400(Text.java:95)
      at javafx.scene.text.Text$TextAttribute$11.computeValue(Text.java:1785)
      at javafx.scene.text.Text$TextAttribute$11.computeValue(Text.java:1777)
      at javafx.beans.binding.ObjectBinding.get(ObjectBinding.java:153)
      at javafx.beans.binding.ObjectExpression.getValue(ObjectExpression.java:50)
      at javafx.beans.property.ObjectPropertyBase.get(ObjectPropertyBase.java:132)
      at com.sun.javafx.scene.control.skin.TextFieldSkin.lambda$new$198(TextFieldSkin.java:233)
      at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
      at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
      at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
      at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
      at javafx.beans.property.ObjectPropertyBase.access$000(ObjectPropertyBase.java:51)
      at javafx.beans.property.ObjectPropertyBase$Listener.invalidated(ObjectPropertyBase.java:233)
      at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
      at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
      at javafx.beans.binding.ObjectBinding.invalidate(ObjectBinding.java:172)
      at javafx.scene.text.Text.impl_geomChanged(Text.java:769)
      at javafx.scene.text.Text.needsTextLayout(Text.java:194)
      at javafx.scene.text.Text.needsFullTextLayout(Text.java:189)
      at javafx.scene.text.Text.access$200(Text.java:95)
      at javafx.scene.text.Text$2.invalidated(Text.java:389)
      at java.lang.ArrayIndexOutOfBoundsException
  Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException
  Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException
  Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException
  Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException
  Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException
  Exception in thread "JavaFX Application Thread"
  [sts] Cancellation request posted...
  [sts] Build cancelled
  [sts] Time taken: 0 min, 39 sec
  [sts] -----------------------------------------------------
  </code></pre>
  
  <p>Oh and also, when I click the red square in the eclipse console to stop the program, it takes ages to respond, why is it so? After all it's just a TextField and a Button...</p>
  
  <p><a href="http://i.stack.imgur.com/GhQay.png" rel="nofollow">screenshot</a></p>