7

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?

belyid
  • 841
  • 2
  • 12
  • 28
  • Could you be a bit more specific? Since Dialog is from java and not from javafx. The Dialog class in java does not have a close() function. If you want to know more about java awt dialog, please check out this tutorial http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html and for javafx http://docs.oracle.com/javafx/2/ui_controls/file-chooser.htm – WonderWorld Feb 24 '15 at 14:36
  • Also this post http://stackoverflow.com/questions/8309981/how-to-create-and-show-common-dialog-error-warning-confirmation-in-javafx-2 should give you some information about javafx dialogs. – WonderWorld Feb 24 '15 at 14:42
  • @WonderWorld I'm using javafx.scene.control.Dialog from JavaFX not the dialog class from Java. But thank you anyway. – belyid Feb 24 '15 at 14:54
  • You must be using some other version of javafx than the latest i assume, because the javafx i have installed does not have javafx.scene.control.Dialog. – WonderWorld Feb 24 '15 at 15:45
  • [`Dialog`](http://download.java.net/jdk9/jfxdocs/index.html?javafx/scene/control/Dialog.html) was introduced in JavaFX 8u40 (at the time of writing this is still about 3 weeks from GA release). – James_D Feb 24 '15 at 15:46

6 Answers6

30

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(); 
David Hedley
  • 344
  • 3
  • 10
18

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();
Michael Marton
  • 595
  • 5
  • 9
2

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();
0

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)

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Means that `dialog.close()` is called after your modal is closed already. – atamanroman Feb 24 '15 at 15:06
  • @WonderWorld Why disabled? The docs say code is blocked while it is "showing", i.e. until it is closed. – James_D Feb 24 '15 at 15:50
  • I read JB Nizet his answer as if you have a button in your main application that pops up a new dialog when you press it, while that dialog is showing the main application is inaccesible aka disabled (Modal). Or im understanding this all wrong. – WonderWorld Feb 24 '15 at 15:53
  • Well, that's true but it's slightly different to what the question (and answer) are about. This is about *code execution blocking* while the dialog is showing, not about access to the other windows in the application. – James_D Feb 24 '15 at 15:56
  • Correct me if i'm wrong. What he means by this answer is that the OP his code that he is showing must be somewhere in the main application because he creates a new dialog. So if the new dialog is shown in blocking fashion then the dialog.close() wouldn't work because code execution is blocked. – WonderWorld Feb 24 '15 at 15:58
  • Yes, that's right. But it is blocked until the dialog is **closed** (as @atamanroman says), not until (as your comment says) the dialog is **disabled**. (Maybe you didn't intend your comment to say that, but in context it is clearly how anyone would interpret it.) I think you probably just misread the original comment. – James_D Feb 24 '15 at 16:09
  • @James_D I do `dialog.close()`in a JavaFX Task `setOnSucceeded`method, but the dialog is not closed. So it's not clear to me, how to achieve to close the dialog. How to close a dialog that is shown by `dialog.showandwait()` programmatically and return control to the "main app"? – marc Dec 21 '15 at 17:17
  • @marc Post a new question with a [MCVE]. Do you really need `showAndWait()`: why wouldn't just `show()` work for you? – James_D Dec 21 '15 at 17:41
  • 1
    Unless the api changed between the beginning and end of 2015, note that show() does not block. showAndWait() does. – Erhannis Sep 07 '16 at 19:03
  • This does not deal with the issue mentioned in the question. The java docs also states "To specify whether you want blocking or non-blocking dialogs, developers simply choose to call Dialog.showAndWait() or Dialog.show() (respectively)". – Hassan Mahmud Jun 21 '18 at 22:19
0

This solution works for me:

dialog.hide();
dialog.close();
 

Advice: The hide() method is in Dialog but not in Alert

xKobalt
  • 1,498
  • 2
  • 13
  • 19
Abrar Malekji
  • 111
  • 1
  • 4
0

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.