Benj
Benj

Reputation: 289

Continue program execution after showAndWait javafx

Hello I have a procedure who load a stage like this :

FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource(ConstantsUI.CHEMIN_VIEW+
                ConstantsUI.ECRAN_CAISSE_FERMEE));
        AnchorPane ecran = (AnchorPane) loader.load();

        // Show the scene containing the root layout.
        Scene scene = new Scene(ecran);

        MainApp.getInstance().getPrimaryStage().setScene(scene);

        genericController = loader.getController();
        genericController.setStage(MainApp.getInstance().getPrimaryStage());

        // on garde la fenêtre en premier plan
        MainApp.getInstance().getPrimaryStage().setMaximized(true);
        MainApp.getInstance().getPrimaryStage().show();
        MainApp.getInstance().getPrimaryStage().showAndWait();
        System.out.println("toto");

And I have an button with this code :

@FXML
public void clickButton() {
    System.out.println("------here-----");
    stage.close();
}

My problem is, that after I clicked on my button, the message "toto" is not visible. Why ?

Thanks.

Upvotes: 0

Views: 866

Answers (1)

fabian
fabian

Reputation: 82461

Calling showAndWait is not allowed for the primary stage.

From the javadoc

Throws:
    [...]
    IllegalStateException - if this method is called on the primary stage.

You could instead use a onHidden event handler:

MainApp.getInstance().getPrimaryStage().setOnHidden(evt -> {
    System.out.println("toto");
});
MainApp.getInstance().getPrimaryStage().show();

Alternatively if you want to run the code after the last window of the application has been closed (asumming you didn't use Platform.setExplicitExit(false)), you could override Application.stop():

@Override
public void stop() throws Exception {
    System.out.println("toto");
}

Upvotes: 1

Related Questions