title: App hangs up or "Not on FX application thread" occurs during app activity
body: <p>The application reacts on actions which occur on gamepad. When button is pressed something happens on UI. But I ran at the issue with app hangs up or "java.lang.IllegalStateException: Not on FX application thread" exception.</p>
  
  <p>In order to fix it I tried the following approaches: <code>Platform.runLater()</code> and <code>Task</code> usage. But it didn't help.</p>
  
  <p>Here is the problem code:</p>
  
  <pre><code>public class GamepadUI extends Application{
  
      private static final int WIDTH = 300;
      private static final int HEIGHT = 213;
  
      private Pane root = new Pane();
      private ImageView iv1 = new ImageView();
  
      private boolean isXPressed = false;
  
      @Override
      public void start(Stage stage) throws Exception {
          initGUI(root);
  
          Scene scene = new Scene(root, WIDTH, HEIGHT);
          stage.setScene(scene);
          stage.setResizable(false);
          stage.show();
      }
  
      public void pressBtn() {
          if(!isXPressed) {
              iv1.setVisible(true);
              isXPressed = true;
          }
      }
  
      public void releaseBtn() {
          if(isXPressed) {
              iv1.setVisible(false);
              isXPressed = false;
          }
      }
  
      private void initGUI(final Pane root) {
          Image image = new Image(Props.BUTTON);
          iv1.setImage(image);
          iv1.setLayoutX(198);
          iv1.setLayoutY(48);
          iv1.setVisible(false);
          root.getChildren().add(iv1);
  
          runTask();
      }
  
      public void runTask() {
          Task task = new Task&lt;Void&gt;() {
              @Override
              protected Void call() throws Exception {
                  initStubGamepad();
                  return null;
              }
          };
  
          new Thread(task).start();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  
      public void initStubGamepad() {
          Random rnd = new Random();
          try {
              while (true) {
                  if (rnd.nextInt(30) == 3) {
                      pressBtn();
                  } else if (rnd.nextInt(30) == 7) {
                      releaseBtn();
                  }
              }
          } catch (Exception ex) {
              System.out.println("Exception: " + ex);
          }
      }
  }
  </code></pre>
  
  <p><code>initStubGamepad()</code> emulates gamepad buttons activity polling. When user presses any button (<code>rnd.nextInt(30) == 3</code>) - an image appears on the UI. When user releases that button (<code>rnd.nextInt(30) == 7</code>) - an image disappears from the UI.</p>
  
  <p>In case above <code>java.lang.IllegalStateException: Not on FX application thread</code> occurs. If you change runTask() to something like this:</p>
  
  <pre><code>Platform.runLater(new Runnable() {
      @Override
      public void run() {
          initStubGamepad();
      }
  });
  </code></pre>
  
  <p>Then app will hang or even main UI won't appear at all, but gamepad activity continues.</p>
  
  <p>What I want is just to show/hide different images when some activity is detected on gamepad (btw, there's no way to monitor gamepad activity except for gamepad polling in an infinite loop). What did I wrong</p>