Reputation: 750
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();
Upvotes: 0
Views: 363
Reputation: 604
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
Upvotes: 4