Search code examples
javafxdialogstage

How to freeze Dialog in JavaFX


I have created a Button (named test) in a stage in JavaFX. Then when you press this button, I show a dialog, where there's an other button(it's not so important this at the moment). Now I would like that the dialog would give me the possibility to freeze the entire application .I mean, I should have not the possibility of back to the previous stage , I can do this, just closing the window with the "X".

Here a picture that explain much better what I want to do. enter image description here

private TextInputDialog dialog;
    private Text actionStatus;
    private final String defaultVal = "Default text";
    private static final String titleTxt = "Esempio JavaFx Dialog";



test.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {

                    Stage dialogstage = new Stage();


                    dialogstage.setTitle(titleTxt); 

                    // Window label
                    Label label = new Label("Text Input Dialog");
                    label.setTextFill(Color.DARKBLUE);
                    label.setFont(Font.font("Calibri", FontWeight.BOLD, 36));
                    HBox labelHb = new HBox();
                    labelHb.setAlignment(Pos.CENTER);
                    labelHb.getChildren().add(label);

                    // Button
                    Button textbtn = new Button("Premi");
//                  textbtn.setOnAction(new TextButtonListener());
                    HBox buttonHb = new HBox(10);
                    buttonHb.setAlignment(Pos.CENTER);
                    buttonHb.getChildren().addAll(textbtn);

                    // Status message text
                    actionStatus = new Text();
                    actionStatus.setFont(Font.font("Calibri", FontWeight.NORMAL, 20));
                    actionStatus.setFill(Color.FIREBRICK);

                    // Vbox
                    VBox vbox = new VBox(30);
                    vbox.setPadding(new Insets(25, 25, 25, 25));;
                    vbox.getChildren().addAll(labelHb, buttonHb, actionStatus);

                    // Scene
                    Scene scene = new Scene(vbox, 500, 250); // w x h
                    dialogstage.setScene(scene);
                    dialogstage.show();

                    // Initial dialog
//                  displayTextDialog();


                    textbtn.setOnAction(new EventHandler<ActionEvent>() {
                        public void handle(ActionEvent e) { 
                        }

                    });


            }
        });

Solution

  • First of all, remove the close button from the Stage:

    dialogstage.initStyle(StageStyle.UNDECORATED);
    

    Then you make your stage modal (A dialog box that blocks input to some other top-level windows in the application, except for windows created with the dialog box as their owner. The modal dialog box captures the window focus until it is closed, usually in response to a button press.)

    dialogstage.initModality(Modality.WINDOW_MODAL);
    

    And define the owner (parent) so the the modality works

    dialogstage.initOwner( parentStage );