0

I try to change icon in my javaFX application. It starts but icon doesn't change in both application top left corner and in task bar. I use this code to change the icon:

Image icon = new Image(new File("window_icon.png").toURI().toString());
stage.getIcons().add(icon);

But it seems to have no effect at all (no exceptions as well). BTW: I'm sure the file exisits. I also tried different images and formats(.png, .ico and .jpg)

(I use ItelliJ 2021.3.2 on windows 10 and create scene with FXMLLoader)

What can be the problem?

Update: tried 16x16, 32x32, 64x64, image's original 256x256

using Image("file:window_icon.png") doesn't change anything

GaussGun
  • 100
  • 1
  • 9
  • 1
    It would be better to not use a _File_ (on disk), but use a _resource_ (on the class path) like `new Image("/window_icon.png");` – Joop Eggen Mar 23 '22 at 21:01
  • 1
    See the guide for [resource lookup in JavaFX](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other), If it continues to fail, check all the troubleshooting steps in that guide to ensure that the image is actually loaded correctly. Image has an [error property](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/image/Image.html#errorProperty()) that you can monitor. Use a standard icon size for the image (e.g. 32x32), or a set of images of standard size multiples. – jewelsea Mar 23 '22 at 21:21
  • 1
    See the [Image constructor javadoc](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/image/Image.html#%3Cinit%3E(java.lang.String)) "url - a resource path, file path, or URL". To tell the constructor to load from a file path rather than a resource path, you need to prefix the file path with `file:`. Though the resource path is recommended as previously noted. Also if you use file, output the value of `new File("window_icon.png").toURI().toString()` and double-check it matches where your icon actually is (I know you said that you already did that). – jewelsea Mar 23 '22 at 21:30
  • 1
    `.ico` format is [not currently directly supported by JavaFX](https://stackoverflow.com/questions/28474791/javafx-2-2-image-support-for-ico), so I don't recommend trying that. – jewelsea Mar 23 '22 at 21:32
  • @jewelsea can Cyrillic symbols in full path cause problems? – GaussGun Mar 23 '22 at 21:35
  • @JoopEggen this causes "Invalid URL or resource not found" – GaussGun Mar 23 '22 at 21:37
  • @jewelsea to your first comment, `stage.getIcons().add(new Image(getClass().getResourceAsStream("/window_icon.png")));` results into NullPointerException – GaussGun Mar 23 '22 at 21:42
  • Either `stage` or the resource stream is null. Check the root directory of your application build output. Likely the `window_icon.png` image is not there. Follow the troubleshooting steps in the [resource lookup guide](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other). Study the [eden coding resource guide](https://edencoding.com/where-to-put-resource-files-in-javafx/). – jewelsea Mar 23 '22 at 23:00
  • 1
    Then the resource was not found. Look in the jar (zip format) and check the case sensitive path. For maven projects the resource file should be in src/main/resources/. – Joop Eggen Mar 24 '22 at 00:46

1 Answers1

1

The solution that worked for me is using ResourceStream. Putting the source image into /src/main/resources/{packagename} folder made this line of code work:

stage.getIcons().add(new Image(getClass().getResourceAsStream("window_icon.png")))

Thanks to @JoopEggen for the hint

GaussGun
  • 100
  • 1
  • 9