How do you set the icon of a Dialog control Java FX/Java 8

You can easily use the icon of your application for the alert-icon by setting your application-window as owner of the alert box:

@FXML
Button buShow;
...

Alert alert = new Alert(AlertType.INFORMATION, "Nice Box.", ButtonType.CLOSE);
alert.initOwner(buShow.getScene().getWindow());   // Alert uses the Windows Icon
alert.show();

Just Do like this:

Alert(AlertType.ERROR, "Erreur de connexion! Verifiez vos Identifiants",FINISH); //Cancel..
setTitle("XNotes FX Erreur");
stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("indiza/XnotesErrorIdz.png")); // To add an icon
showAndWait();

Here is the result

enter image description here

**My friends, is it computer science that we do? : No, we do crafts **


There's an excellent tutorial here by Marco Jakob, where you can find not only how to use dialogs, but also how to solve your problem.

Both for the new dialogs (in JDK8u40 early versions or with openjfx-dialogs with JDK 8u25), or for those in ControlsFX, in order to set the icon of your dialog, you can use this solution:

Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(
    new Image(this.getClass().getResource("<image>.png").toString()));

This code snippet shows how to use a ProgressDialog, from ControlsFX, and set an icon for the dialog:

@Override
public void start(Stage primaryStage) {

    Service<Void> service = new Service<Void>() {
        @Override protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override protected Void call() throws InterruptedException {
                    updateMessage("Message . . .");
                    updateProgress(0, 10);
                    for (int i = 0; i < 10; i++) {
                        Thread.sleep(300);
                        updateProgress(i + 1, 10);
                        updateMessage("Progress " + (i + 1) + " of 10");
                    }
                    updateMessage("End task");
                    return null;
                }
            };
        }
    };

    Button btn = new Button("Start Service");
    btn.setOnAction(e -> {
        ProgressDialog dialog = new ProgressDialog(service);
        dialog.setTitle("Progress Dialog");
        dialog.setHeaderText("Header message");
        Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(this.getClass().getResource("<image>.png").toString()));
        service.start();
    });

    Scene scene = new Scene(new StackPane(btn), 300, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
}