...">

Javafx.scene.control.Dialog <R> will not close when you press "x"

If I just create an empty class that extends from javafx.scene.control.Dialog<R> , it will not close when I click the "x" button in the upper right corner.

How to implement this behavior? The API seems to tell me that I need to implement a close button. But in my case, I do not want the close button, I just want to close the window with the x button or by pressing ESC. Is it possible?

+5
source share
4 answers

To get around this, you can 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 box meets both your requirements and the fact that you can close them using the close icon of the embedded window system window, as well as the requirements for the JavaFX dialog box, which includes the close button in the dialog box, so that the close icon works.

Alternatively, you can use Stage with showAndWait instead of Dialog . A Stage without any buttons turned on is closed using the window close window icon.

+10
source

The workaround from @eckig or @jewelsea works very well. 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 restrictions on this use, but it worked for me. And I recommend initializing the window immediately after initializing the dialog, as shown above.

+12
source

To quote Api Docs :

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

  • If the dialog box has only one button or

  • If there are several buttons in the dialog box, so far one of them meets one of the following requirements:

    • A button is of type ButtonType, ButtonData is of type ButtonData.CANCEL_CLOSE.
    • A button has a ButtonType, ButtonData returns true when calling ButtonData.isCancelButton ().

    ...

So, add at least one button or several 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 by HeavyweightDialog , which is not a public API, therefore it is not an extension point.

+6
source

In my Dialog<ButtonType> I use what @vbargl said:

 Window window = alert.getDialogPane().getScene().getWindow(); window.setOnCloseRequest(event -> window.hide()); 

It closes the dialog box, but it brings me the error of no value.

To avoid this, I also check result.get() and result.isPresent() .

+1
source

All Articles