Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I unable to programmatically close a Dialog on JavaFX?

Tags:

java

javafx

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?

like image 275
belyid Avatar asked Feb 24 '15 14:02

belyid


People also ask

What is DialogPane 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 .

Are JavaFX dialogs modal?

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).


1 Answers

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(); 
like image 109
David Hedley Avatar answered Sep 20 '22 14:09

David Hedley