I would like to block the owner window for a popup in JavaFX.
I initialize my popup like this:
popUp = new Popup();
popUp.getContent().add(content);
popUp.show(pane.getScene().getWindow());
With this, I can still work in the first window (pane window). I would like to disable this action and I would like the user just works in the popup.
How to do this ?
Thanks.
Use a Stage instead of a Popup.
Before showing the stage, invoke stage.initModality as either APPLICATION_MODAL or WINDOW_MODAL, as appropriate. Also invoke stage.initOwner to the parent window of your new stage so that it will appropriately block it for the WINDOW_MODAL
case.
Stage stage = new Stage();
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(pane.getScene().getWindow());
stage.setScene(new Scene(content));
stage.show();
Thanks, optimal solution: example with FXML load file:
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("DialogView.fxml"));
primaryStage.initModality(Modality.APPLICATION_MODAL); // 1 Add one
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.initOwner(primaryStage.getScene().getWindow());// 2 Add two
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With