5

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 window and in the taskbar. where I have to put the image.

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.

vijayk
  • 2,633
  • 14
  • 38
  • 59
  • possible duplicate of [How can I set an icon to the window of a JavaFX application?](http://stackoverflow.com/questions/16783737/how-can-i-set-an-icon-to-the-window-of-a-javafx-application) – jewelsea Nov 27 '13 at 17:07

2 Answers2

8

You should place the icon in your jar or classpath and then load it through a resource function. E.g. if you place it to your bin folder, into the package where your class is, then the following should work:

primaryStage.getIcons().add(new Image(this.getClass().getResourceAsStream("sicadcam.png")));
Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
  • I was put the image file in com.test.action package it run but dont show icon? – vijayk Nov 28 '13 at 06:00
  • where i want to place the image file? – vijayk Nov 28 '13 at 06:32
  • Try it with `getResourceAsStream("/com/test/action/sicadcam.png");` – Udo Klimaschewski Nov 28 '13 at 08:10
  • @Udo.thanks its wroking.....if I want to invoke .ico file instead of .jpg/.png then how to invoke..in case of .ico icon not reflected. – vijayk Nov 28 '13 at 08:27
  • 1
    .ico formats are not natively supported in Java, as far as I know. You should convert the image using an image tool. Or you can use an external library to manually read it, see: http://stackoverflow.com/questions/11400707/how-to-read-an-ico-format-picture-in-java – Udo Klimaschewski Nov 28 '13 at 08:48
  • With FX 17 the icon shows only top left on app window, but not in taskbar. Any ideas how to fix? – horvoje May 29 '23 at 17:07
0

I suspect that the image is not handled as a resource and not getting into your Jar file. Can you verify if it is there? (You can do that by Total Commander for example by pressing Ctrl+PgDown to go into the archive).

Another reason might be that NetBeans using a different run configuration and classpath. Where is your image? If it is in the package root (i.e., the folder which contains your top-level package), you can probably access it somehow like: ImageIO.read(getClass().getResourceAsStream("/sicadcam.png")).

Hope that helps something.

rlegendi
  • 10,466
  • 3
  • 38
  • 50