If I just create an empty class extending from javafx.scene.control.Dialog<R>
, it won't close when I'm pressing the "x" button in the top right corner.
How do I implement this behaviour? The API seems to tell me that I need to implement a close button. But in my case I don't want a close button, I just want to close the window with the x button or by pressing ESC. Is this possible?
To quote the Api Docs:
So either add at least one button or multiple buttons, and one of them is of type
ButtonData.CANCEL_CLOSE
, for example:Edit:
This behavior is implemented in
javafx.scene.control.FXDialog.requestPermissionToClose(Dialog<?>)
, but the realFXDialog
shown isHeavyweightDialog
which is not public API so not really an extension point.The workaround from @eckig or @jewelsea works pretty fine. But I would use something like this:
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.
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.
In my
Dialog<ButtonType>
I'm using what @vbargl said:It closes the dialog, but it's bringing me a no value present error.
To avoid it, I'm also checking
result.get()
and thatresult.isPresent()
.