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?
Be default, to quit the program is to let the user click the standard window close button, typically represented by an X in the upper-right corner of the window's title bar.
To make a button do something in JavaFX you need to define the executable code usually by using a convenience method like setOnAction() . Then, when the button is clicked, JavaFX does the heavy lifting of fetching that pre-defined code, and executing it on the JavaFX Application thread.
DialogPane should be considered to be the root node displayed within a Dialog instance. In this role, the DialogPane is responsible for the placement of headers , graphics , content , and buttons .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With