Create Dialogs with Default Images in JavaFX (Info/Warning/Error)

5.4k views Asked by At

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?

3

There are 3 answers

3
ItachiUchiha On

You do not need to create custom JavaFX dialogs for Info/Warning/Error, since JavaFX already have created Alerts for you.

The Alert class subclasses the Dialog class, and provides support for a number of pre-built dialog types that can be easily shown to users to prompt for a response.

You can create different types of Alerts, depending on the AlertType, it will embed the necessary image.

For Information alert use :

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("An Information Dialog");
alert.setContentText("Information Message");
alert.showAndWait();

Similarly, for Warning alert, you can use

AlertType.WARNING

and for Error alert, you can use :

AlertType.ERROR
0
Alberto On

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);
0
SedJ601 On

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

enter image description here
Dialog example:

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

enter image description here

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.