Reputation: 879
I made a JavaFX application on Netbeans and I put this code for setting the icon to the window
primaryStage.getIcons().add(new Image("file:sicadcam.png"));
and when I run the project from Netbeans, it works ok: the icon appears on the top left corner of the window and in the taskbar. The image is in the root directory of the project.
When I clean and build the project, it generates two installers: one exe and one msi; and when I install the application and open it, the window doesn't have the icon sicadcam.png, it has the default java logo icon.
How or where can I set the path of the image so that when I install the application the icon appears.
Upvotes: 3
Views: 6427
Reputation: 159281
Update April 2023
There are various places icons can show, e.g. in a taskbar or dock, in an installer, or in an installed application settings manager OS, icons for associated files, desktop shortcut icons, etc. Where and how they show depends on the OS.
JavaFX inbuilt support for icons is quite limited.
If you have a stage with a title, you can get the icons from the stage and add an image there to form an icon to be displayed next to the stage title. This is less useful than it might seem because the only place the icon is seen is in the window frame, not in the dock on OS X or an installer, program manager, or app store (I am not sure of the Windows taskbar, as I did not test that).
Modern OS X applications in general don't show icons in their window title bars, so, in some sense, at least for OS X, placing icons in the title area is a bit of a UI anti-pattern.
The best way to get the icons for the application to show up in the standard places the OS will show them is to use a tool such as jpackage
, to build an installable application package that includes the required icons (in various OS recommended sizes and formats) within the installation package. The way and manner to do that are intricate and platform-specific, and I advise consulting the jpackage
documentation and the OS-specific recommendations on application icons. The packaging process that includes application icons in the installation package is not part of the JavaFX API, framework and toolset. jpackage
is a standard tool shipped with all modern JDKs (e.g. JDK 17+.
Documentation on various icon formats and resources used in packaging using jpackage
.
The rest of this answer is largely obsolete and outdated, but just left for historical reasons.
This is only a partial answer to the question as I have been unable to generate a self-contained package which shows the icon for the installed application in the Windows taskbar. I may have missed a step, have an environmental issue or the icon configuration for self-contained applications may be slightly buggy and may be fixed in later releases. Testing was on: jdk7u21, NetBeans 7.3, Win7, InnoSetup.
Get the icon image from a resource rather than a file.
For example, if you place the image in the source directory of your application class:
new Image(MyApplication.class.getResource("sicadcam.png").toExternalForm());
For an installed application, this will only set the icon displayed in the top left corner of the screen. To set the icon for the taskbar, desktop etc, following the instructions in the Self-Contained Packaging section of the JavaFX Deployment Guide is supposed to get you there.
For example to get an icon for the installer on my Windows 7 for my self-contained application, I needed to place the icon in a <netbeansprojectdir>/package/windows/<myappname>.ico
(needs to be a .ico for the installer packager to pick it up) file AND ensure my ant path was configured correctly as detailed in How to change JavaFx native Bundle exe icon (hopefully that manual ant configuration won't be required in later NetBeans/JavaFX releases).
Download the Ensemble sample application from Oracle and see how the package directory is laid out there for platform specific icons. Unfortunately when I tried building Ensemble from the command line, I was also unable to get Windows 7 to use the Ensemble icon in the taskbar when Ensemble was installed and executed as a self-contained application.
Ensure your build system copies your icon into the jar file containing your application. To check this, change into the app directory of your installed application and run jar tvf <yourappjar>.jar
=> it should show the location of the icon resource file in the jar.
Upvotes: 3
Reputation:
you can it by using getClass().getResourceAsStream("path.png") the getResourceAsStream("path") is return an input stram for path of any file you are need it for example for icon win.getIcons().add(new Image(getClass().getResourceAsStream("path.png"))); and if you are makking an jar file is run with out Exception
Upvotes: 0