在关闭时重定向到另一个窗口

时间:2018-08-17 10:10:06

标签: java javafx stage

我具有成功登录后要调用的用户窗口。用户阶段由LoginController类中的以下代码调用。

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窗口。

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


}

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

1 个答案:

答案 0 :(得分:0)

这是我设法解决问题的方法: 我在userController中创建了静态舞台对象:

public static Stage userStage;

并更改了如下调用用户窗口的登录方法:

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

而且有效!