Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Richard McDonald
Added: Aug 16, 2016 12:41 PM
Views: 1512
Tags: dialog javafx
  1. Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
  2. alert.setTitle("Current project is modified");
  3. alert.setContentText("Save?");
  4. ButtonType okButton = new ButtonType("Yes", ButtonBar.ButtonData.YES);
  5. ButtonType noButton = new ButtonType("Yes", ButtonBar.ButtonData.NO);
  6. ButtonType cancelButton = new ButtonType("Yes", ButtonBar.ButtonData.CANCEL_CLOSE);
  7. alert.getButtonTypes().setAll(okButton, noButton, cancelButton);
  8. alert.showAndWait().ifPresent(type -> {
  9.         if (type == ButtonType.OK) {
  10.         } else if (type == ButtonType.NO) {
  11.         } else {
  12.         }
  13. });
  14.  

1 comment

Hi, theres couple of mistakes.
1) All buttons marked as Yes... Yes Yes Yes. Need to assign different captions. "Yes, shure" "No no no" "It does not matter"

ButtonType okButton = new ButtonType("Yes, shure", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("No no no", ButtonBar.ButtonData.NO);
ButtonType cancelButton = new ButtonType("It does not matter", ButtonBar.ButtonData.CANCEL_CLOSE);

2) when You process result do use declared objects (not standard types):

alert.showAndWait().ifPresent(type -> {
if (type == okButton) { // do something
} else if (type == noButton) { // do something
} else { // do something
}
});

Write a comment