title: 'Not on FX application thread' error when starting thread from dialog with loaded FXML as content
body: <p>I currently have the following situation:</p>
  
  <p>I have created a <strong>JavaFX</strong> application, which contains a screen from which I open a dialog (just by clicking a button on the screen). Then, the user gives input and clicks the apply button. The user input is then sent to a method, which opens some sort of progress dialog (for showing the user the status of the synchronization, which is not important for the problem). I will call this dialog <strong>'MyDialog'</strong>. MyDialog is built with the following code:</p>
  
  <pre><code>Dialog&lt;Void&gt; dialog = new Dialog&lt;&gt;();
  dialog.initOwner(null);
  dialog.initStyle(StageStyle.UNDECORATED);
  dialog.setHeaderText("Nieuw product synchroniseren...");
  dialog.setResizable(false);
  
  //Load dialog FXML file into the Pane
  FXMLLoader fxmlloader = new FXMLLoader();
  fxmlloader.setLocation(getClass().getResource("dialogs/MyDialogContent.fxml"));
  try {
      dialog.getDialogPane().setContent(fxmlloader.load());
  } catch (IOException e) {
      Functions.createExceptionDialog(e);
  }
  MyDialogContentController childController = fxmlloader.getController();
  
  final ButtonType canceledButtonType = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
  dialog.getDialogPane().getButtonTypes().add(canceledButtonType);
  </code></pre>
  
  <p>This works just fine. The ProgressBar, which is shown in MyDialog, indicated the progress of a <strong>task</strong>. The task is started from a <strong>thread</strong>. This thread is started from the controller of the upper screen. From within the task, I would like to show an additional dialog at some point, to get some user validation. I will call this dialog <strong>'AlertDialog'</strong>. This is the code for that part (which is placed in the Controller of the upper screen, and not in the Controller of MyDialog):</p>
  
  <pre><code>Task&lt;Object&gt; task = new Task&lt;Object&gt;() {
  
      @Override
      protected Object call() {
          //Show choice dialog
          Alert alert = new Alert(AlertType.CONFIRMATION);
          alert.initOwner(null);
          alert.initStyle(StageStyle.UNDECORATED);
  
          ButtonType buttonTypeOne = new ButtonType("One");
          ButtonType buttonTypeTwo = new ButtonType("Two");
          ButtonType buttonTypeThree = new ButtonType("Three");
          ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
  
          alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree, buttonTypeCancel);
  
          Optional&lt;ButtonType&gt; result = alert.showAndWait();
  
          if (result.get() == buttonTypeOne){
              //User chose "One";
          } else if (result.get() == buttonTypeTwo) {
              // ... user chose "Two"
          } else if (result.get() == buttonTypeThree) {
              // ... user chose "Three"
          } else {
              // ... user chose CANCEL or closed the dialog
          }
      }
  }
  </code></pre>
  
  <p>Unfortunately, the AlertDialog is not showing, and I get the following error:</p>
  
  <pre><code>Exception in thread "Thread-10" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-10
  </code></pre>
  
  <p><strong>I already tried the following solutions:</strong></p>
  
  <ul>
  <li>Place the AlertDialog code in the MyDialogController, and then call it from the task.</li>
  <li>Start the thread from MyDialogController, instead of starting in from the upper screen controller. And then call it by 'childController.thread'.</li>
  </ul>
  
  <p>Both of these solutions did not work. I expect it has something to do with loading the FXML file in the DialogPane, and therefore with the thread and from where it is started.</p>
  
  <p>So the questions in this situation are:</p>
  
  <ol>
  <li><p><strong>Why do I get this error?</strong></p></li>
  <li><p><strong>Has the error something to do with the AlertDialog not showing?</strong></p></li>
  <li><p><strong>Should my approach for this part of the code be different? (e.g. without loading an external FXML file in a dialog)</strong></p></li>
  </ol>
  
  <p>Any help is greatly appreciated!</p>