简体   繁体   English

你如何设置一个对话框控件的图标 Java FX/Java 8

[英]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).我可能遗漏了一些非常明显的东西,但我不知道如何为 Dialog 组件设置图标(更准确地说是 ProgressDialog)。 I know how to do that for a Stage:我知道如何为 Stage 做到这一点:

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

But I don't find anything for the Dialog family.但我没有为 Dialog 家族找到任何东西。 And somehow, setting the Stage Icon does not influence the Dialog Icon.不知何故,设置舞台图标不会影响对话框图标。

Thanks谢谢

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. Marco Jakob 在这里有一个很棒的教程,您不仅可以在其中找到如何使用对话框,还可以找到如何解决您的问题。

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 thissolution :无论是对于新对话框(在 JDK8u40 早期版本中还是在 JDK 8u25 中使用 openjfx - dialogs),或者对于那些在ControlsFX中的对话框,为了设置对话框的图标,您都可以使用以下解决方案

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:此代码片段显示了如何使用 ControlsFX 中的ProgressDialog并为对话框设置图标:

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

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这是结果

在此处输入图片说明

**My friends, is it computer science that we do? **我的朋友们,我们学的是计算机科学吗? : No, we do crafts ** :不,我们做工艺品**

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

This is a method that I include in my JavaFX projects, simply calling this method and passing the Alert as a parameter will set both the title bar icon and the header graphic.这是我在 JavaFX 项目中包含的一种方法,只需调用此方法并将 Alert 作为参数传递即可设置标题栏图标和标题图形。

public class Msg {

public void showInfo(String title, String header, String message) {
    Alert alertShowInfo = new Alert(Alert.AlertType.INFORMATION);
    addDialogIconTo(alertShowInfo); //add icon and header graphic
    alertShowInfo.setTitle(title);
    alertShowInfo.setHeaderText(header);
    alertShowInfo.setContentText(message);
    alertShowInfo.showAndWait();
}

//this adds images to Alert
public void addDialogIconTo(Alert alert) {
    // Add custom Image to Dialog's title bar
    final Image APPLICATION_ICON = new Image("icon.png");
    Stage dialogStage = (Stage) alert.getDialogPane().getScene().getWindow();
    dialogStage.getIcons().add(APPLICATION_ICON);

    // Add custom ImageView to Dialog's header pane.        
    final ImageView DIALOG_HEADER_ICON = new ImageView("icon.png");
    DIALOG_HEADER_ICON.setFitHeight(48); // Set size to API recommendation.
    DIALOG_HEADER_ICON.setFitWidth(48);
    alert.getDialogPane().setGraphic(DIALOG_HEADER_ICON);                       
    }
}

Then, in whatever class I wish to use the Alert, it will already have the customized icon and header graphic.然后,在我希望使用警报的任何类中,它都已经具有自定义图标和标题图形。

public static void main(String[] args){
        Msg msg = new Msg();

        // Alert will now include custom icon and header graphic.
        msg.showInfo("Sucess!", "Program succeeded", "Now exiting program");
} 

Just similar to any dialog, instead this is inside a button handler.与任何对话框类似,而是在按钮处理程序中。

    Alert alert = new Alert(
        AlertType.WARNING,
        "Alert message here.",
        ButtonType.OK
    );
    alert.initOwner(((Button)event.getSource()).getScene().getWindow());
    alert.setTitle("Alert window title");
    alert.showAndWait();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM