java fx 内置图标,如何设置Dialog控件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

解决方案

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值