Block parent stage until child stage closes

10,267

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);
Share:
10,267
Anshul Parashar
Author by

Anshul Parashar

Updated on June 14, 2022

Comments

  • Anshul Parashar
    Anshul Parashar almost 2 years

    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();
    
  • Anshul Parashar
    Anshul Parashar over 10 years
    its not working...when i am added stage.initModality(Modality.WINDOW_MODAL); stage.initOwner((Stage)btnSearch.getScene().getWindow()); these lines with above code.its not working..how can i get primarystage from main class..
  • Anshul Parashar
    Anshul Parashar over 10 years
    @uiuk can you help me once more..pls give me some suggestion at here stackoverflow.com/questions/19969567/…