title: How to dynamically create stage from another class in JavaFX
body: <p>Supposedly I have an extended application class to launch from another class</p>
  <pre><code>public class BasicApp extends Application {
      public static CountDownLatch instanceLatch = new CountDownLatch(1);
      public static BasicApp instance;
  
      public synchronized static BasicApp getInstance() {
          if(instance == null) {
              try {
                  new Thread(() -&gt; Application.launch(BasicApp.class)).start();
                  instanceLatch.await();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          return instance;
      }
  
      public BasicApp () {
          instance = this;
          instanceLatch.countDown();
      }
  .
  .
  .
  </code></pre>
  <p>And I want to create multiple instances of the same stage from another class, so I might create a list of stage</p>
  <pre><code>    private static List&lt;Stage&gt; stages;
  
      @Override
      public void start(Stage primaryStage) throws Exception {
          for (Stage st : stages) {
              st.show();
          }
      }
  .
  .
  .
  </code></pre>
  <p>so I define a static method to instantiate the stage from another class</p>
  <pre><code>    public static Stage createStage(String data) {
          Stage stage = new Stage();
          Scene scene = new Scene(new Label(data));
          stage.setScene(scene);
  
          stages.add(stage);
  
          return stage;
      }
  }
  </code></pre>
  <p>However, when I tried to launch the class and create the stage,</p>
  <pre><code>public class MainClass {
      public static void main (String[] args) {
          BasicApp app = getInstance();
          app.createStage(&quot;Test&quot;);
      }
  }
  </code></pre>
  <p>It threw an exception that says Stage cannot be created outside the FX Application thread.
  <code>java.lang.IllegalStateException: Not on FX application thread;</code></p>
  <p>The app instance has no problem, yet,
  How do I achieve to create the instance of the same stage without getting this error?</p>