Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block owner window Java FX

Tags:

java

javafx-2

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.

like image 366
Kiva Avatar asked Mar 25 '13 22:03

Kiva


2 Answers

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();
like image 86
jewelsea Avatar answered Oct 27 '22 10:10

jewelsea


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

    }
like image 4
user3434059 Avatar answered Oct 27 '22 10:10

user3434059