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

后端 未结 4 2085
臣服心动
臣服心动 2021-02-07 05:15

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 Stag

4条回答
  •  孤街浪徒
    2021-02-07 05:43

    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(".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 service = new Service() {
            @Override protected Task createTask() {
                return new Task() {
                    @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(".png").toString()));
            service.start();
        });
    
        Scene scene = new Scene(new StackPane(btn), 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

提交回复
热议问题