关闭时重定向到另一个 window

Redirecting to another window on close

我有用户 window,我在成功登录后调用它。用户阶段由 LoginController class.

中的以下代码调用
public void login(ActionEvent event) {
    try {
        if (loginModel.isLoggedIn(txtLogin.getText(), txtPassField.getText())) {
            lblDBStatus.setText("Username and password are correct");

            String fxmlFile = "/fxml/User.fxml";
            LOG.debug("Loading FXML for User view from: {}", fxmlFile);

            LOG.info("Hiding login page");
            ((Node) event.getSource()).getScene().getWindow().hide();


            Stage stage = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane rootNode = loader.load(getClass().getResource(fxmlFile).openStream());
            UserController userController = (UserController) loader.getController();
            userController.getUserName(txtLogin.getText());

            LOG.info("Showing User page");
            Scene scene = new Scene(rootNode);
            scene.getStylesheets().add("/styles/user-controller.css");

            stage.setTitle("WindOptimizer");
            stage.setScene(scene);
            stage.show();


        } else {
            lblDBStatus.setText("Username and password are incorrect");

        }
    } catch (SQLException | IOException e) {
        e.printStackTrace();
    }
}

现在我有另一个事件处理程序,它将我从 UserController 重定向到它并正在创建 Turbines window。

public void openTurbinesTab(ActionEvent event) {

    try {
        String fxmlFile = "/fxml/WindTurbines.fxml";
        LOG.debug("Loading FXML for Turbines view from: {}", fxmlFile);

        LOG.info("Hiding user page");
        ((Node) event.getSource()).getScene().getWindow().hide();


        Stage stage = new Stage();
        FXMLLoader loader = new FXMLLoader();
        Pane rootNode = loader.load(getClass().getResource(fxmlFile).openStream());
        TurbinesController turbinesController = (TurbinesController) loader.getController();
        turbinesController.getStationName(stationCB.getValue().toString());

        LOG.info("Showing Turbines page");
        Scene scene = new Scene(rootNode);
        scene.getStylesheets().add("/styles/turbines-controller.css");

        stage.setTitle("WindOptimizer");
        stage.setScene(scene);
        stage.show();

        stage.setOnCloseRequest((WindowEvent event1) -> {


        });

    } catch (IOException e) {
        e.printStackTrace();
    }


}

在涡轮机中执行一些操作后 window 我想保存此数据(就像 getUserName 或 getStationName)并在关闭涡轮机选项卡后重定向回用户 window。如您所见,我很确定我应该使用 stage.setOnCloseRequest((WindowEvent event1),但不幸的是,我不知道应该在此方法中提供什么才能成功调用回用户 window。

我是这样解决问题的: 我在 userController 中创建了静态阶段对象:

public static Stage userStage;

并更改了调用用户 Window 的登录方法,如下所示:

public void login(ActionEvent event) {
    try {
        if (loginModel.isLoggedIn(txtLogin.getText(), txtPassField.getText())) {
            lblDBStatus.setText("Username and password are correct");

            String fxmlFile = "/fxml/User.fxml";
            LOG.debug("Loading FXML for User view from: {}", fxmlFile);

            LOG.info("Hiding login page");
            ((Node) event.getSource()).getScene().getWindow().hide();


            userStage = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane rootNode = loader.load(getClass().getResource(fxmlFile).openStream());
            UserController userController = (UserController) loader.getController();
            userController.getUserName(txtLogin.getText());

            LOG.info("Showing User page");
            Scene scene = new Scene(rootNode);
            scene.getStylesheets().add("/styles/user-controller.css");

            userStage.setTitle("WindOptimizer");
            userStage.setScene(scene);
            userStage.show();


        } else {
            lblDBStatus.setText("Username and password are incorrect");

        }
    } catch (SQLException | IOException e) {
        e.printStackTrace();
    }
}

然后在 userController 中我调用了 openTurbines 选项卡并将焦点设置在它上面:

public void openTurbinesTab(ActionEvent event) {

    try {
        String fxmlFile = "/fxml/WindTurbines.fxml";
        LOG.debug("Loading FXML for Turbines view from: {}", fxmlFile);

        LOG.info("Hiding user page");

        Stage stage = new Stage();
        FXMLLoader loader = new FXMLLoader();
        Pane rootNode = loader.load(getClass().getResource(fxmlFile).openStream());

        TurbinesController turbinesController = (TurbinesController) loader.getController();
        turbinesController.getStationName(stationCB.getValue().toString());

        LOG.info("Showing Turbines page");
        Scene scene = new Scene(rootNode);
        scene.getStylesheets().add("/styles/turbines-controller.css");

        stage.setTitle("WindOptimizer");
        stage.setScene(scene);
        stage.initOwner(LoginController.userStage);
        stage.initModality(Modality.WINDOW_MODAL);
        stage.showAndWait();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

它正在运行!