How to make the opened window must close at first before back to the main?

喜欢而已 提交于 2019-12-11 18:08:50

问题


I want to make that second window must close first like alert dialog. what should I add to this code when Button clicked:

Parent parent = FXMLLoader.load(getClass().getResource("view/sec_win.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();

回答1:


There's a property called stage.initOwner(Stage stg) that allows this to happen.

Example:

public class JavaFXApplication4 extends Application {

@Override
public void start(Stage stage) {
   Button jb = new Button("Click");
   jb.setOnMouseClicked(new EventHandler() {
        @Override
           public void handle(Event event) {
               makeAnotherStage(stage);
           }
       });

       GridPane gp = new GridPane();
       gp.getChildren().add(jb);
       Scene s = new Scene(gp);

       stage.setScene(s);
       stage.show();

    }

    private void makeAnotherStage(Stage st){
        Stage s = new Stage();

        GridPane gp = new GridPane();
        Label l = new Label("Second Stage");
        gp.getChildren().add(l);
        Scene sc = new Scene(gp);

        s.initOwner(st);                        <------- initOwner
        s.initModality(Modality.WINDOW_MODAL);  <------- Modality property

        s.setScene(sc);
        s.requestFocus();
        s.show();
    }
}

Oracle Documentation on Modality: https://docs.oracle.com/javafx/2/api/javafx/stage/Modality.html



来源:https://stackoverflow.com/questions/34247327/how-to-make-the-opened-window-must-close-at-first-before-back-to-the-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!