This sample of code shows but doesn't close a javafx.scene.control.Dialog on JavaFx:
Dialog<Void> dialog = new Dialog<Void>();
dialog.show();
dialog.close();
or
Dialog<Void> dialog = new Dialog<Void>();
dialog.show();
dialog.hide();
Why?
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 .
Those in-built JavaFX Dialog and Alert classes also have initOwner and initModality and showAndWait methods, so that you can set the modality for them as you wish (note that, by default, the in-built dialogs are application modal).
I'm not sure why the response above has been marked as an answer as it clearly doesn't answer the question. The underlying issue appears to be that it is not possible to programmatically close a dialog box that doesn't have a Close/Cancel button:
Dialog box opens, but doesn't close:
Dialog<Void> dialog = new Dialog<Void>();
dialog.show();
dialog.close();
To close, add a cancel button to it just before you close it:
Dialog<Void> dialog = new Dialog<Void>();
dialog.show();
// Add dummy cancel button
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL);
// Dialog will now close
dialog.close();
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