How to return value from a stage before closing it?

前端 未结 4 652
遥遥无期
遥遥无期 2020-11-27 23:26

First of all, sorry for the bad english.

Here is the case:

I have a \"main stage\" where i press a button to open a \"second stage\" where i have a table, th

4条回答
  •  爱一瞬间的悲伤
    2020-11-28 00:26

    There is some error in 4baad4's example. If the method in the controller is iCanGetDataBeforeClose, then that's what should be called:

    String someValue = controller.iCanGetDataBeforeClose();
    

    But even that didn't work right for me. I actually got this to work without using setOnCloseRequest at all. In the form controller, I had a method like this:

    public boolean getCompleted() {
        return this.finished;
    }
    

    Then in the form calling it:

    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("myView.fxml"));
    AnchorPane pane = (AnchorPane) loader.load();
    myViewController controller = loader.getController();
    Scene scene = new Scene(pane);
    Stage stage = new Stage();
    stage.setScene(scene);
    stage.showAndWait();
    if (controller.getCompleted()){
        doStuff();
    }
    

    One might think that since the stage had exited that the controller would throw a null reference exception, but it didn't, and it returned the correct response.

    This solution works and is simplest proposed IMHO.

提交回复
热议问题