Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9105957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:13:28+00:00 2026-06-17T02:13:28+00:00

I have a MainWindowFx class like below. It basically creates a simple JavaFX GUI.

  • 0

I have a MainWindowFx class like below. It basically creates a simple JavaFX GUI.

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);
    }
}

Now I want to use an instance of this object in another class:

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();
  }    

}

But when I run this I ran into this error:

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.<init>(Scene.java:287)
    at javafx.scene.Scene.<init>(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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:13:29+00:00Added an answer on June 17, 2026 at 2:13 am

    I’ve had this problem several times and there is a fairly easy way to resolve it.

    First of all let me introduce you to the Mediator pattern, basically you want to create a class that has the relationship with all your GUI classes

    (I.e the different GUI classes do not have their own instance of each other instead all of them has the same reference to the Mediator).

    That was a sidetrack now to your question.

    In order to change window you need to pass the Stage of which the new window should be placed upon because of this your code needs only a minor change:

    Now I do not often do this but in your case, I will make an exception the following code consists of a class that you can “Copy Paste” into your program and use that will fix the problem after the code I will explain exactly what I did:

    Mediator

    public class Mediator extends Application {
    
        private DrawAppFx daf;
        private MainWindowFX mainWindow;
        private Stage primaryStage;
        public Mediator(){
            daf = new DrawAppFx(this);
            mainWindow = new MainWindowFx(this);
    
        }
        public static void main(String[] args){
            launch(args);
        }
        @Override
        public void start(Stage stage) throws Exception {
            primaryStage = stage;
            mainWindow.start(primaryStage);
        }
        public void changeToDaf(){
            daf.start(primaryStage);
        }
    }
    

    Now each of the DrawAppFx and MainWindowFx must have a constructor that passes a Mediator object and therefore have a “Has-a” relationship with the mediator

    The reason behind this is that the mediator pattern is now in control and should you create more windows it is easy to implement just add them to the mediator and add a method for which the mediator can change to that window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a window which is like this <Window x:Class=pharmacy_Concept.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350
I have cut my problem down to simple lines of Scala + JavaFX 2's
I have a custom class that I would like to bind a WPF TreeView
I am using JavaFX 2 with Scala. I have the class Application extends javafx.application.Application
I have a serial port class, and I would like to control send/receive via
I have a simple custom window (XAML below). When first loaded, it will add
I have the following dependency property in my MainWindow class (inherits from WPF's Window)
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
I have the following code: <Window x:Class=kkk.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350 Width=525> <Window.Resources> <Style
I have two widgets defined as follows class mainWindow : public QWidget { Q_OBJECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.