Search code examples
javajavafx-2

Block owner window Java FX


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.


Solution

  • 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();