Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 602
Jonatan Stenbacka Avatar asked Aug 17 '15 10:08

Jonatan Stenbacka


People also ask

How do you close a JavaFX stage?

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.

Do something when button is pressed JavaFX?

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.

What is dialog pane in JavaFX?

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 .


1 Answers

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.

like image 160
vbargl Avatar answered Oct 01 '22 06:10

vbargl