Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I might be missing something very obvious, but I can't find out how to set the Icon for a Dialog component (ProgressDialog to be more precise). I know how to do that for a Stage:

this.primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon/Logo.png")));

But I don't find anything for the Dialog family. And somehow, setting the Stage Icon does not influence the Dialog Icon.

Thanks

like image 624
BlackLabrador Avatar asked Jan 16 '15 02:01

BlackLabrador


3 Answers

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();
}
like image 106
José Pereda Avatar answered Sep 19 '22 22:09

José Pereda


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 **

like image 10
Envy Hion Avatar answered Sep 23 '22 22:09

Envy Hion


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();
like image 5
Roland N P Avatar answered Sep 20 '22 22:09

Roland N P