简体   繁体   English

在 javaFX 中为 FileChooser 设置应用程序图标

[英]Set application icon for FileChooser in javaFX

After I start a FileChooser window in my javaFX application in windows the application icon in the taskbar shows a java icon and not the icon of the main window.在 windows 中的 javaFX 应用程序中启动 FileChooser 窗口后,任务栏中的应用程序图标显示一个 java 图标,而不是主窗口的图标。 Is there a possibility to select an application icon for a FileChooser instance?是否可以为 FileChooser 实例选择应用程序图标?

Thanks for your answeres!感谢您的回答!

It is possible to do so, but apparently only when you have a visible parent stage.这样做是可能的,但显然只有当您有一个可见的父阶段时。

Provided that stage in the following example is visible, you can do this:如果以下示例中的stage可见,您可以执行以下操作:

stage.getIcons().add(new Image("http://i.imgur.com/1M3UaZy.png"));
FileChooser fileChooser = new FileChooser();
File result = fileChooser.showSaveDialog(stage);

This opens the file chooser as a child of the given stage, which has the given icon.这将打开文件选择器作为给定阶段的子级,它具有给定的图标。

I stepped through the JavaFX source code with a debugger (Oracle Java 8u72 on Windows x64), and there is not single point in the Java code where the icon could be set.我使用调试器(Windows x64 上的 Oracle Java 8u72)逐步浏览了 JavaFX 源代码,并且 Java 代码中没有可以设置图标的单点。 The parent window handle is passed into a native method, where it then the icon probably gets resolved somewhere in the Win32 windowing code.父窗口句柄被传递到本机方法中,然后图标可能会在 Win32 窗口代码中的某处解析。

The issue with the above solution is, that it won't work out of the box when you are using FXML-controllers.上述解决方案的问题是,当您使用 FXML 控制器时,它无法开箱即用。 That troubled me for a while, but finally I found a solution for that, which I'd like to share with you.这让我困扰了一段时间,但最终我找到了解决方案,我想与您分享。

First you need to assign you topmost pane wihtin your fxml-file an id, for instance:首先,您需要为 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">

Now you can use this id in your controller to create the link:现在您可以在控制器中使用此 ID 来创建链接:

@FXML
private AnchorPane primaryStageAnchorPane;

That whas the hard part.这就是困难的部分。 All you need to do now is to get the window-attributes of the linked pane (which are the ones from the primary stage of your application-file):您现在需要做的就是获取链接窗格的窗口属性(这些属性来自应用程序文件的初级阶段):

Stage stage = (Stage) primaryStageAnchorPane.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
File tempFolder = fileChooser.showOpenDialog(stage);

This will overtake the main application's icon for your filechooser-window.这将取代文件选择器窗口的主应用程序图标。 As a side-effect your file-chooser won't appear as a seperate item in the task-bar anymore.作为副作用,您的文件选择器将不再作为单独的项目出现在任务栏中。

Worked for me, hopes it helps somebody out there :)为我工作,希望它可以帮助那里的人:)

This is an ugly hack based on RAnders00 answer .这是基于RAnders00 answer的丑陋黑客。 It calls show before the showSaveDialog call, plus it calls stage.initStyle(StageStyle.UNDECORATED) so we don't get an unwanted window.它在showSaveDialog调用之前调用show ,另外它调用stage.initStyle(StageStyle.UNDECORATED)所以我们不会得到一个不需要的窗口。

In addition, it only worked using resource icons, and not the url example icon.此外,它只能使用资源图标,而不是 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();

Only tested on windows 10.仅在 Windows 10 上测试。

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

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