在 javaFX 中为 FileChooser 设置应用程序图标
Set application icon for FileChooser in javaFX
在 javaFX 应用程序中启动 FileChooser window 后,windows 任务栏中的应用程序图标显示 java 图标,而不是主要 window。是否有可能 select FileChooser 实例的应用程序图标?
感谢您的回答!
可以这样做,但显然只有当你有一个 visible parent stage 时。
只要下面例子中的stage
可见,就可以这样做:
stage.getIcons().add(new Image("http://i.imgur.com/1M3UaZy.png"));
FileChooser fileChooser = new FileChooser();
File result = fileChooser.showSaveDialog(stage);
这会打开文件选择器作为给定阶段的子级,它具有给定的图标。
我用调试器(Oracle Java 8u72 on Windows x64)单步调试了 JavaFX 源代码,Java 中没有单点可以设置图标的代码。父 window 句柄被传递到本机方法,然后图标可能在 Win32 windowing 代码中的某处得到解析。
上述解决方案的问题是,当您使用 FXML 控制器时,它无法开箱即用。这困扰了我一段时间,但最终我找到了解决办法,我想与大家分享。
首先,您需要为您的 fxml 文件中最顶层的窗格分配一个 ID,例如:
<AnchorPane prefHeight="563.0" prefWidth="442.0" scaleShape="false" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:id="primaryStageAnchorPane" fx:controller="application.MyController">
现在您可以在控制器中使用此 ID 来创建 link:
@FXML
private AnchorPane primaryStageAnchorPane;
这才是最难的部分。您现在需要做的就是获取 linked 窗格的 window 属性(它们来自您的应用程序文件的初级阶段):
Stage stage = (Stage) primaryStageAnchorPane.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
File tempFolder = fileChooser.showOpenDialog(stage);
这将取代您的文件选择器的主应用程序图标-window。作为副作用,您的文件选择器将不再作为单独的项目出现在任务栏中。
为我工作,希望它能帮助那里的人:)
这是一个基于 的丑陋黑客。它在 showSaveDialog
调用之前调用 show
,而且它调用 stage.initStyle(StageStyle.UNDECORATED)
,所以我们不会得到不需要的 window.
此外,它只能使用资源图标,而不是 url 示例图标。
// For me it only worked with resource icons, nor url example icon
stage.getIcons().add(new Image(getClass().getClassLoader().getResourceAsStream("icon.png")));
FileChooser fileChooser = new FileChooser();
// App icon show hack
stage.initStyle(StageStyle.UNDECORATED); // Remove unwanted window (no buttons no window)
stage.show();
File result = fileChooser.showSaveDialog(stage);
// Close app icon
stage.hide();
仅在 windows 10.
上测试
在 javaFX 应用程序中启动 FileChooser window 后,windows 任务栏中的应用程序图标显示 java 图标,而不是主要 window。是否有可能 select FileChooser 实例的应用程序图标?
感谢您的回答!
可以这样做,但显然只有当你有一个 visible parent stage 时。
只要下面例子中的stage
可见,就可以这样做:
stage.getIcons().add(new Image("http://i.imgur.com/1M3UaZy.png"));
FileChooser fileChooser = new FileChooser();
File result = fileChooser.showSaveDialog(stage);
这会打开文件选择器作为给定阶段的子级,它具有给定的图标。
我用调试器(Oracle Java 8u72 on Windows x64)单步调试了 JavaFX 源代码,Java 中没有单点可以设置图标的代码。父 window 句柄被传递到本机方法,然后图标可能在 Win32 windowing 代码中的某处得到解析。
上述解决方案的问题是,当您使用 FXML 控制器时,它无法开箱即用。这困扰了我一段时间,但最终我找到了解决办法,我想与大家分享。
首先,您需要为您的 fxml 文件中最顶层的窗格分配一个 ID,例如:
<AnchorPane prefHeight="563.0" prefWidth="442.0" scaleShape="false" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:id="primaryStageAnchorPane" fx:controller="application.MyController">
现在您可以在控制器中使用此 ID 来创建 link:
@FXML
private AnchorPane primaryStageAnchorPane;
这才是最难的部分。您现在需要做的就是获取 linked 窗格的 window 属性(它们来自您的应用程序文件的初级阶段):
Stage stage = (Stage) primaryStageAnchorPane.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
File tempFolder = fileChooser.showOpenDialog(stage);
这将取代您的文件选择器的主应用程序图标-window。作为副作用,您的文件选择器将不再作为单独的项目出现在任务栏中。
为我工作,希望它能帮助那里的人:)
这是一个基于 showSaveDialog
调用之前调用 show
,而且它调用 stage.initStyle(StageStyle.UNDECORATED)
,所以我们不会得到不需要的 window.
此外,它只能使用资源图标,而不是 url 示例图标。
// For me it only worked with resource icons, nor url example icon
stage.getIcons().add(new Image(getClass().getClassLoader().getResourceAsStream("icon.png")));
FileChooser fileChooser = new FileChooser();
// App icon show hack
stage.initStyle(StageStyle.UNDECORATED); // Remove unwanted window (no buttons no window)
stage.show();
File result = fileChooser.showSaveDialog(stage);
// Close app icon
stage.hide();
仅在 windows 10.
上测试