-2

I'm trying to do a window manager, in which I can create a Stage and that stage is the only possible stage that I can have. This window manager can recognize if I'm on the login page or in the registration page, or in whatever other GUI. This window manager is done with the Singleton Pattern (and I need to do with it...)

This is the (ugly) Login GUI (Not finished yet, it needs DB connections, etc...)

public class LoginGUI {

    static int winID = 1;
    Stage stage;
    WindowManagerGUI winInstance;

    public int getID() {
        return winID;
    }

    public Scene createScene() {
        VBox root = new VBox();
        Scene scene = new Scene(root,500,500);

        Text title = new Text();
        title.setText("Benvenuto in Social Music!");

        TextField text1 = new TextField();
        PasswordField text2 = new PasswordField();

        Button loginBTN = new Button("Accedi");
        Button registerBTN = new Button("Registrati");
        Button recoverDataBTN = new Button("Recupera Dati");

        loginBTN.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event) {

            }
        });

        registerBTN.setOnAction(new EventHandler<ActionEvent>(){

            public void handle(ActionEvent event) {
                try {
                    WindowManagerGUI.setStage(RegisterGUI.winID);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        recoverDataBTN.setOnAction(new EventHandler<ActionEvent>(){

            public void handle(ActionEvent event) {

            }
        });

        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(title, text1, text2, loginBTN, registerBTN, recoverDataBTN);
        return scene;
    }

    public void displayInterface() {

        Stage stage = WindowManagerGUI.getStage();
        stage.setScene(createScene());
    }
}

This is the register GUI

public class RegisterGUI  {

    Stage stage = WindowManagerGUI.getStage();
    WindowManagerGUI winInstance;
    static int winID = 2;
    Scene scene;

    public int getID() {
        return winID;
    }

    public Scene createScene() {
        VBox root = new VBox();
        this.scene = new Scene(root,500,500);

        Button registerBTN = new Button("Registrati");

        registerBTN.setOnAction(new EventHandler<ActionEvent>(){

            public void handle(ActionEvent event) {

            }
        });

        root.getChildren().addAll(registerBTN);

        scene = new Scene(root,500,500);
        return scene;
    }

    public void displayInterface(Stage stage) {
        stage.setScene(createScene());
        stage.show();
    }
}

And this is the Window manager

public class WindowManagerGUI {

    private static WindowManagerGUI winInstance = null;
    private static Stage stage = null;

    public static WindowManagerGUI getWindowManager() {

        if (WindowManagerGUI.winInstance == null)
            WindowManagerGUI.winInstance = new WindowManagerGUI();
        return winInstance;
    }

    public static Stage getStage() {

        if(WindowManagerGUI.stage == null)
            WindowManagerGUI.stage = new Stage();
        return stage;
    }

    public static void setStage(int ID) {

        switch(ID) {
        case 1:
            LoginGUI login = new LoginGUI();
            login.displayInterface();
        //case 2:
        //  RegisterGUI register = new RegisterGUI();
        //  register.displayInterface(stage);
        }
    }

    public static void main(String[] args){

        WindowManagerGUI.setStage(1);
        WindowManagerGUI.stage.show();
    }
}

Now, I've got this error when I run this.

Exception in thread "main" java.lang.ExceptionInInitializerError at
javafx.stage.Window.(Window.java:1209) at
javafx.stage.Stage.(Stage.java:239) at
javafx.stage.Stage.(Stage.java:227) at
logic.utils.WindowManagerGUI.getStage(WindowManagerGUI.java:23) at
logic.boundary.LoginGUI.displayInterface(LoginGUI.java:78) at
logic.utils.WindowManagerGUI.setStage(WindowManagerGUI.java:33) at
logic.utils.WindowManagerGUI.main(WindowManagerGUI.java:42) Caused by:
java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main at
com.sun.glass.ui.Application.checkEventThread(Application.java:443) at com.sun.glass.ui.Screen.setEventHandler(Screen.java:285) at
com.sun.javafx.tk.quantum.QuantumToolkit.setScreenConfigurationListener(QuantumToolkit.java:674) at javafx.stage.Screen.(Screen.java:79) ... 7 more

How to fix this?

Abra
  • 19,142
  • 7
  • 29
  • 41

1 Answers1

0

In the WindowManagerGUI.getStage() static method, you are instantiating a new Stage object. You are calling this method from the main thread, but you will receive an IllegalStateException as noted in the API documentation (https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Stage.html) if the Stage object is instantiated in any other thread besides the JavaFX Application thread.

While looking through some tutorials online (here: http://tutorials.jenkov.com/javafx/stage.html, and here: https://www.tutorialspoint.com/javafx/javafx_application.htm), it seems that one needs to create a class that extends Application and override the start(Stage stage) method in order to execute on the JavaFX Application thread.

I hope this helps!

danielehmig
  • 116
  • 2
  • Either that or use `Platform.startup` which makes shutting down the toolkit a bit more complicated... – fabian Dec 16 '19 at 20:26
  • Read the comment section of [this](https://learnjavafx.typepad.com/weblog/2009/04/javafx-tips-and-tricks-using-singleton-pattern-to-share-a-model.html) – SedJ601 Dec 16 '19 at 21:00
  • https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – SedJ601 Dec 16 '19 at 21:03