GaussGun
GaussGun

Reputation: 110

JavaFX window icon doesn't change

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

Upvotes: 0

Views: 528

Answers (1)

GaussGun
GaussGun

Reputation: 110

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

Upvotes: 1

Related Questions