Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block parent stage until child stage closes

I have a controller on which an action event of my button opens a child stage. The issue is when I close the parent stage, the child stage also closes. I want to prevent parent stage from closing as long as child stage is open.

enter image description here

URL url = getClass().getResource("Message.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(url);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
root = (Parent)fxmlLoader.load(url.openStream());            
Stage stage = new Stage();
//stage.initStyle(StageStyle.UNDECORATED);
//stage.setFullScreen(true);
stage.setTitle("Welcome User");
stage.setScene(new Scene(root, 675, 
stage.show();
like image 587
Anshul Parashar Avatar asked Jan 27 '26 02:01

Anshul Parashar


1 Answers

Set the followings:

stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(primaryStage);

You can get the primaryStage by putting it to static variable in main class:

public static Stage primaryStage;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    ...
}

then

stage.initOwner(MainApp.primaryStage);
like image 123
Uluk Biy Avatar answered Jan 30 '26 00:01

Uluk Biy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!