1

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?

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
Nidia
  • 21
  • 1
  • 4
  • Nidia you can find the standard dialog icons in `/com/sun/javafx/scene/control/skin/modena` in `jfxrt.jar` which ships with Java 8. As they are undocumented and under a `com.sun` hierarchy there is no guarantee they will be present at the same location in a future version of JavaFX (though it is highly likely they will be). – jewelsea Jun 09 '15 at 11:23

3 Answers3

4

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);
Community
  • 1
  • 1
Alberto
  • 1,569
  • 1
  • 22
  • 41
1

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.

Community
  • 1
  • 1
SedJ601
  • 12,173
  • 3
  • 41
  • 59
0

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
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • 2
    That is not what I want. I have a custom dialog and I want to use the same icon the Alert class uses. – Nidia Jun 09 '15 at 10:08
  • 2
    Ahh, I misunderstood your question. I am not aware of any standard way to load them but since the images are located in the jfxrt.jar, you can load them by using the path `/com/sun/javafx/scene/control/skin/modena/dialog-error.png` or `dialog-warning.png` or `dialog-information.png`. – ItachiUchiha Jun 09 '15 at 11:26
  • @ItachiUchiha Thanks for posting the location to the images. This helped me solve an issue I was having. I had set the alert graphic to a progressBar, but on failure I needed to reset the image back to it's default. Here is what I did: ImageView error = new ImageView(this.getClass().getResource("/com/sun/javafx/scene/control/skin/modena/dialog-error.png").toString()); alert.setGraphic(error); – Dan Whitehouse Nov 17 '16 at 03:46