How to create and show common dialog (Error, Warning, Confirmation) in JavaFX 2.0?

74

How do I create and show common dialogs (Error, Warning, Confirmation) in JavaFX 2.0? I can't find any "standard" classes like Dialog, DialogBox, Message or something.

This question is tagged with dialog javafx-2

~ Asked on 2011-11-29 11:28:50

The Best Answer is


95

Recently released JDK 1.8.0_40 added support for JavaFX dialogs, alerts, etc. For example, to show a confirmation dialog, one would use the Alert class:

Alert alert = new Alert(AlertType.CONFIRMATION, "Delete " + selection + " ?", ButtonType.YES, ButtonType.NO, ButtonType.CANCEL);
alert.showAndWait();

if (alert.getResult() == ButtonType.YES) {
    //do stuff
}

Here's a list of added classes in this release:

~ Answered on 2015-03-05 20:38:54


51

EDIT: dialog support was added to JavaFX, see https://stackoverflow.com/a/28887273/1054140


There were no common dialog support in a year 2011. You had to write it yourself by creating new Stage():

Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);

VBox vbox = new VBox(new Text("Hi"), new Button("Ok."));
vbox.setAlignment(Pos.CENTER);
vbox.setPadding(new Insets(15));

dialogStage.setScene(new Scene(vbox));
dialogStage.show();

~ Answered on 2011-11-29 12:09:13


Most Viewed Questions: