title: JavaFX Application: is the primary stage in .start() any special?
body: <p>This is the base fxml file:</p>
  
  <pre><code>&lt;BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
      minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0"
      xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
      fx:controller="com.github.parboiled1.grappa.debugger.basewindow.BaseWindowUi"&gt;
      &lt;top&gt;
          &lt;MenuBar BorderPane.alignment="CENTER"&gt;
              &lt;Menu mnemonicParsing="false" text="File"&gt;
                  &lt;MenuItem mnemonicParsing="false"
                      text="New window" onAction="#newWindowEvent"/&gt;
                  &lt;MenuItem mnemonicParsing="false"
                      text="Close" onAction="#closeWindowEvent"/&gt;
              &lt;/Menu&gt;
          &lt;/MenuBar&gt;
      &lt;/top&gt;
      &lt;center&gt;
          &lt;Label text="Hello"/&gt;
      &lt;/center&gt;
  &lt;/BorderPane&gt;
  </code></pre>
  
  <p>The <code>BaseWindowUi</code> class is simplistic:</p>
  
  <pre><code>public class BaseWindowUi
  {
      private BaseWindowPresenter presenter;
  
      public void init(final BaseWindowPresenter presenter)
      {
          this.presenter = presenter;
      }
  
      @FXML
      void newWindowEvent(final ActionEvent event)
      {
          presenter.handleNewWindow();
      }
  
      @FXML
      public void closeWindowEvent(final ActionEvent event)
      {
          presenter.handleCloseWindow();
      }
  }
  </code></pre>
  
  <p>And so is the presenter:</p>
  
  <pre><code>public final class BaseWindowPresenter
  {
      private final BaseWindowFactory windowFactory;
  
      public BaseWindowPresenter(final BaseWindowFactory windowFactory)
      {
          this.windowFactory = windowFactory;
      }
  
      public void handleCloseWindow()
      {
          windowFactory.close(this);
      }
  
      public void handleNewWindow()
      {
          windowFactory.createWindow();
      }
  }
  </code></pre>
  
  <p><code>BaseWindowFactory</code> is an interface with only one concrete implementation:</p>
  
  <pre><code>// Interface...
  public interface BaseWindowFactory
  {
      void createWindow(Stage stage);
  
      default void createWindow()
      {
          createWindow(new Stage());
      }
  
      void close(BaseWindowPresenter presenter);
  }
  
  // Implementation...
  public final class DefaultBaseWindowFactory
      implements BaseWindowFactory
  {
      private static final AlertFactory ALERT_FACTORY = new AlertFactory();
      private static final URL MAIN_WINDOW_FXML;
  
      static {
          try {
              MAIN_WINDOW_FXML = DefaultBaseWindowFactory.class.getResource(
                  "/baseWindow.fxml");
              if (MAIN_WINDOW_FXML == null)
                  throw new IOException("base window fxml not found");
          } catch (IOException e) {
              ALERT_FACTORY.showError("Fatal error", "cannot load base FXML", e);
              throw new ExceptionInInitializerError(e);
          }
      }
  
      private final Map&lt;BaseWindowPresenter, Stage&gt; windows
          = new HashMap&lt;&gt;();
  
      private final AtomicInteger windowCount = new AtomicInteger();
  
      @Override
      public void createWindow(final Stage stage)
      {
          final FXMLLoader loader = new FXMLLoader(MAIN_WINDOW_FXML);
          final Pane pane;
          try {
              pane = loader.load();
          } catch (IOException e) {
              ALERT_FACTORY.showError("Fatal error", "unable to create window",
                  e);
              return;
          }
  
          final BaseWindowPresenter presenter = new BaseWindowPresenter(this);
  
          final BaseWindowUi ui = loader.getController();
          ui.init(presenter);
  
          stage.setScene(new Scene(pane));
          stage.setTitle("window " + windowCount.getAndIncrement());
  
          windows.put(presenter, stage);
  
          stage.show();
      }
  
      @Override
      public void close(final BaseWindowPresenter presenter)
      {
          windows.get(presenter).close();
      }
  }
  </code></pre>
  
  <p>Finally, here's the class implementing <code>Application</code>:</p>
  
  <pre><code>public final class TestApp
      extends Application
  {
      private final BaseWindowFactory factory = new DefaultBaseWindowFactory();
  
      @Override
      public void start(final Stage primaryStage)
          throws Exception
      {
          factory.createWindow(primaryStage);
      }
  
      public static void main(final String... args)
      {
          launch(args);
      }
  }
  </code></pre>
  
  <p>All of these work; I can open new windows, close the one created from the "primary stage", the application exits correctly when all windows are closed etc.</p>
  
  <p>So, then, how is the primary stage special? </p>
  
  <p>What is more, the documentation for <code>Application</code> says this:</p>
  
  <blockquote>
    <p>JavaFX creates an application thread for running the application start method, processing input events, and running animation timelines. Creation of JavaFX Scene and Stage objects as well as modification of scene graph operations to live objects (those objects already attached to a scene) must be done on the JavaFX application thread. </p>
  </blockquote>
  
  <p>My code works currently but it does pretty much nothing at all; won't I eventually run into the problem of code which should run in the application thread but does not? are all <code>new Stage()</code>s automatically aware of the application thread?</p>