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?
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?
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();
A shorter (but not less "hacky" method) is to use a dialog of a specific type (other than Void) and set a (arbitrary) result directly before hiding it, e.g.:
Dialog<Boolean> dialog = new Dialog<Boolean>();
dialog.show();
...
// for closing
dialog.setResult(Boolean.TRUE);
dialog.close();
As mentioned you should add button
Dialog<Void> dialog = new Dialog<Void>();
dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
dialog.showAndWait();
if that design makes no sense at all you can hide button
Dialog<Void> dialog = new Dialog<Void>();
dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
Node closeButton = dialog.getDialogPane().lookupButton(ButtonType.CLOSE);
closeButton.setVisible(false);
dialog.showAndWait();
also you can use directly window
Dialog<Void> dialog = new Dialog<Void>();
Window window = dialog.getDialogPane().getScene().getWindow();
window.setOnCloseRequest((e) -> {
dialog.hide();
});
dialog.showAndWait();
last solution is create your own dialog window
Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.setResizable(false);
VBox root = new VBox();
Scene dialogScene = new Scene(root);
Button close = new Button("Close");
close.setOnAction((e) -> {
dialog.close();
});
root.getChildren().addAll(close);
dialog.setScene(dialogScene);
dialog.showAndWait();
From the javadoc:
More often than not, dialogs are shown in a modal and blocking fashion. 'Modal' means that the dialog prevents user interaction with the owning application whilst it is showing, and 'blocking' means that code execution stops at the point in which the dialog is shown
(emphasis mine)
This solution works for me:
dialog.hide();
dialog.close();
Advice: The hide()
method is in Dialog
but not in Alert
You can close the dialog by accessing the window its DialogPane
is embedded in:
Window window = dialog.getDialogPane().getScene().getWindow();
window.setOnCloseRequest(event -> dialog.close());
Now the dialog should close like every other window.