javafx.scene.control.Dialog<R> won't close on pressing "x"

The workaround from @eckig or @jewelsea works pretty fine. But I would use something like this:

// Somewhere in code
Dialog<?> dialog = new Dialog<>();
Window    window = dialog.getDialogPane().getScene().getWindow();
window.setOnCloseRequest(event -> window.hide());

I do not know any constrains of this use, but it worked for me. And I recommend initialize window right after dialog initialization, like above.


To work-around this, you could add a hidden close button to the dialog.

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class DialogClosure extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        Button openDialog = new Button("Open Dialog");
        openDialog.setOnAction(event -> {
            Dialog dialog = new Dialog();
            dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
            Node closeButton = dialog.getDialogPane().lookupButton(ButtonType.CLOSE);
            closeButton.managedProperty().bind(closeButton.visibleProperty());
            closeButton.setVisible(false);
            dialog.showAndWait();
        });

        stage.setScene(new Scene(openDialog));
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Then the dialog meets both your requirement of being able to be closed via the native windowing system's window close icon as well as the JavaFX Dialog requirement of including a close button in the dialog for the close icon to work.

Alternately, you could use a Stage with showAndWait instead of a Dialog. A Stage without any included buttons is closable using the windowing system's close window icon.


To quote the Api Docs:

JavaFX dialogs can only be closed 'abnormally' (as defined above) in two situations:

  1. When the dialog only has one button, or

  2. When the dialog has multiple buttons, as long as one of them meets one of the following requirements:

    1. The button has a ButtonType whose ButtonData is of type ButtonData.CANCEL_CLOSE.
    2. The button has a ButtonType whose ButtonData returns true when ButtonData.isCancelButton() is called.

    ...

So either add at least one button or multiple buttons, and one of them is of type ButtonData.CANCEL_CLOSE, for example:

Dialog<ButtonType> dialog = new Dialog<>();
dialog.getDialogPane().getButtonTypes().add(new ButtonType("Got it!", ButtonData.CANCEL_CLOSE));
dialog.setContentText("test");
dialog.showAndWait();

Edit:

This behavior is implemented in javafx.scene.control.FXDialog.requestPermissionToClose(Dialog<?>), but the real FXDialog shown is HeavyweightDialog which is not public API so not really an extension point.