I am creating a JavaFX Dialog
and want to use the default icons
for Info/Warning/Error.
In Swing, I can get the Information icon this way:
UIManager.getIcon("OptionPane.informationIcon")
How can I do the same in JavaFX?
I asked for this some days ago. I use this code to make labels with default icons:
Label img = new Label();
img.getStyleClass().addAll("alert", "error", "dialog-pane");
dialog.setGraphic(img);
If you get a copy of the images you can use these ideas.
Alert example:
@FXML void handleHelpButton(ActionEvent event){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Help");
alert.setHeaderText("Help");
alert.setGraphic(new ImageView(this.getClass().getResource("img/help.png").toString()));
alert.setContentText("Place the cursor over a button for hint.");
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));
alert.showAndWait();
}
ChoiceDialog<String> dialog = new ChoiceDialog<>(currentFullscreenSetting, choices);
dialog.setTitle("Settings");
dialog.setHeaderText("Settings");
dialog.setContentText("Fullscreen on startup: ");
dialog.setGraphic(new ImageView(this.getClass().getResource("img/settings.png").toString()));
Stage stage2 = (Stage) dialog.getDialogPane().getScene().getWindow();
stage2.getIcons().add(new Image(this.getClass().getResource("img/settings.png").toString()));
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
This part of both examples is tricky. I noticed that this would not work unless I had the image in the same folder as the .fxml and controller.java files or in a folder that is in the same folder has the files mentioned. You might have to play with you file location. In my example it appears that my setGraphic and getIcons images are in the same folder, but they are not.
stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));
My file structure looks like:
PlanningChart
css
img
planningchart
img
The second img folder holds the images for stage.getIcons.add(). The images could also be probably use to for setGraphic. I did not try it.
You do not need to create custom
JavaFX dialogs
forInfo/Warning/Error
, since JavaFX already have created Alerts for you.You can create different types of
Alerts
, depending on the AlertType, it will embed the necessary image.For
Information
alert use :Similarly, for
Warning
alert, you can useand for
Error
alert, you can use :