title: Multiple independent stages in JavaFX
body: <p>Is there a way to launch multiple independent stages in JavaFX? By independent I mean that the stages are all created from the main thread.</p>
  
  <p>At the moment my application is more or less an algorithm where I would like to plot some charts and tables during execution (mainly to check whether the results are correct/ to debug). </p>
  
  <p>The problem is that I cannot figure out how to create and show multiple stages independently, i.e. I would like to do something like this</p>
  
  <pre><code>public static void main(){
      double[] x = subfunction.dosomething();
      PlotUtil.plot(x); //creates a new window and shows some chart/table etc.
      double[] y = subfunction.dosomethingelse();
      PlotUtil.plot(y); //creates a new window and shows some chart/table etc.
      .....
  }
  </code></pre>
  
  <p>which would allow to use <code>PlotUtil</code> as one would use the plotting functions in other scripting languages (like Matlab or R).</p>
  
  <p>So the main question is how to "design" <code>PlotUtils</code>? So far I tried two things</p>
  
  <ol>
  <li>PlotUtils uses Application.launch for each plot call (creating a new stage with a single scene every time) --> does not work as <code>Application.launch</code> can only be invoked once.</li>
  <li>Create some kind of "Main Stage" during the first call to PlotUtils, get a reference to the created Application and start subsequent stages from there --> does not work as using <code>Application.launch(SomeClass.class)</code> I am not able to get a reference to the created Application instance.</li>
  </ol>
  
  <p>What kind structure/design would allow me to implement such a PlotUtils function?</p>
  
  <p><strong>Update 1:</strong> </p>
  
  <p>I came up with the following idea and was wondering whether there are any major mistakes in this solution.</p>
  
  <p>Interface to be implemented by all "Plots"</p>
  
  <pre><code>public abstract class QPMApplication implements StageCreator {
     @Override
     public abstract  Stage createStage();
  }
  </code></pre>
  
  <p>Plotting functionality:</p>
  
  <pre><code>public class PlotStage {
      public static boolean toolkitInialized = false;
  
      public static void plotStage(String title, QPMApplication stageCreator) {
          if (!toolkitInialized) {
              Thread appThread = new Thread(new Runnable() {
                  @Override
                  public void run() {
                      Application.launch(InitApp.class);
                  }
              });
              appThread.start();
          }
  
          while (!toolkitInialized) {
              try {
                  Thread.sleep(100);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          Platform.runLater(new Runnable() {
              @Override
              public void run() {
                  Stage stage = stageCreator.createStage();
                  stage.show();
              }
          });
      }
  
      public static class InitApp extends Application {
          @Override
          public void start(final Stage primaryStage) {
              toolkitInialized = true;
          }
      }
  }
  </code></pre>
  
  <p>Using it:</p>
  
  <pre><code>public class PlotStageTest {
  
      public static void main(String[] args) {
  
          QPMApplication qpm1 = new QPMApplication() {
              @Override
              public Stage createStage() {
                  Stage stage = new Stage();
                  StackPane root = new StackPane();
                  Label label1 = new Label("Label1");
                  root.getChildren().add(label1);
                  Scene scene = new Scene(root, 300, 300);
                  stage.setTitle("First Stage");
                  stage.setScene(scene);
                  return stage;
              }
          };
  
          PlotStage.plotStage(qpm1);
  
          QPMApplication qpm2 = new QPMApplication() {
              @Override
              public Stage createStage() {
                  Stage stage = new Stage();
                  StackPane root = new StackPane();
                  Label label1 = new Label("Label2");
                  root.getChildren().add(label1);
                  Scene scene = new Scene(root, 300, 200);
                  stage.setTitle("Second Stage");
                  stage.setScene(scene);
                  return stage;
              }
          };
  
          PlotStage.plotStage(qpm2);
  
          System.out.println("Done");
  
      }
  }
  </code></pre>