title: Using a JavaFx application instance from another class
body: <p>I have a MainWindowFx class like below. It basically creates a simple <code>JavaFX</code> GUI.</p>
  
  <pre><code>package drawappfx;
  
  
  import javafx.stage.Stage;
  import javafx.scene.Scene;
  import javafx.scene.control.TextArea;
  import javafx.scene.control.Button;
  import javafx.scene.layout.BorderPane;
  import javafx.scene.layout.VBox;
  import javafx.application.Application;
  import javafx.geometry.Insets;
  import javafx.scene.control.TextAreaBuilder;
  
  
  /**
   *
   * @author Hieu
   */
  public class MainWindowFX extends Application{
      public static final int DEFAULT_WIDTH = 600;
      public static final int DEFAULT_HEIGHT = 600;
  
      private int width;
      private int height;
  
      private Scene scene;
      private TextArea messageView;
      private Button quitButton;
      private BorderPane layout;
      private Stage primaryStage;
  
      @Override
      public void start(Stage primaryStage) {
          System.out.println("Started building GUI....");
          this.buildGUI();
          System.out.println("Finished building GUI");
          this.primaryStage = primaryStage;
          primaryStage.setTitle("Hello World!");
          primaryStage.setScene(this.scene);
          primaryStage.show();
          System.out.println("Where the hell are you?");
      }
  
      public Scene getScene() {
          return this.scene;
      }
  
      public BorderPane getBorderPane() {
          return this.layout;
      }
  
      public Stage getPrimaryStage() {
          return this.primaryStage;
      }
  
      public void buildGUI() {
          System.out.println("Before layout");
          this.layout = new BorderPane();
          System.out.println("Before vbox");
          this.layout.setBottom(this.addVBox());
          System.out.println("before new scene");
          this.scene = new Scene(this.layout, DEFAULT_WIDTH, DEFAULT_HEIGHT);
          System.out.println("after new scene");
      }
  
  
      public VBox addVBox() {
         VBox vbox = new VBox();
         vbox.setPadding(new Insets(15, 12, 15, 12));
  
         // message box
         this.messageView = TextAreaBuilder.create()
                 .prefRowCount(5)
                 .editable(false)
                 .build();
  
         // quit button
         this.quitButton = new Button("Quit");
         this.quitButton.setPrefSize(100, 20);
         System.out.println("think of a good message?");
         vbox.getChildren().addAll(this.messageView, this.quitButton);
         System.out.println("before returning vbox");
         return vbox;
      }
  
      public void postMessage(final String s) {
          this.messageView.appendText(s);
      }
  }
  </code></pre>
  
  <p>Now I want to use an instance of this object in another class:</p>
  
  <pre><code>package drawappfx;
  
  import java.io.InputStreamReader;
  import java.io.Reader;
  import javafx.scene.layout.BorderPane;
  
  public class DrawAppFx
  {
    public static void main(String[] args)
    {
      final MainWindowFX main = new MainWindowFX();
      BorderPane layout = main.getBorderPane();
      Reader reader = new InputStreamReader(System.in);
      Parser parser = new Parser(reader,layout,main);
      main.start(main.getPrimaryStage());
      parser.parse();
    }    
  
  }
  </code></pre>
  
  <p>But when I run this I ran into this error:</p>
  
  <pre><code>java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at com.javafx.main.Main.launchApp(Main.java:658)
      at com.javafx.main.Main.main(Main.java:805)
  Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = main
      at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:237)
      at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:397)
      at javafx.scene.Scene.&lt;init&gt;(Scene.java:287)
      at javafx.scene.Scene.&lt;init&gt;(Scene.java:226)
      at drawappfx.MainWindowFX.buildGUI(MainWindowFX.java:74)
      at drawappfx.MainWindowFX.start(MainWindowFX.java:47)
      at drawappfx.DrawAppFx.main(DrawAppFx.java:39)
      ... 6 more
  Java Result: 1
  </code></pre>
  
  <p>I've done some searches on this and guessed that it has something to do with threading... but I still have no idea. Any suggestions?</p>