0

I'm fairly new to graphical libraries, in this case, JavaFX, and I wanted to add an icon to my project. So I googled and found two different methods: The first one was pretty straightforward:

stage.getIcons().add(new Image("300px-Wikibooks-logo.png"));

That just put out an error message, that the URL isn't valid.

The second method is very similar:

stage.getIcons().add(new Image(getClass().getResourceAsStream("300px-Wikibooks-logo.png")));

This one doesn't fail. It compiles and runs just fine, but the icon isn't showing up. I even tried every single location in my program and I used every "Copy Path" option. I also read that there should be a / in front of the pathname, but I didn't work with it either.

For better visualization, here is my file tree:


Aufgabenblatt5

---.idea

---src

-----main

-------java

---------de.ab5.abgabe5

-----------(here are all my classes)

-------resources

---------de.ab5.abgabe5

-----------(here was the image file while it worked)

----target


Maybe someone can help.

Malte
  • 11
  • 1
  • If you inspect (unzip) the jar, is there that image file present in the jar? If not, then you must include it in the jar when you compile the app. – corneliu Dec 27 '21 at 15:17
  • Add the line: `System.out.println(getClass().getResourceAsStream("300px-Wikibooks-logo.png"));` before you create the image and ensure that the value it outputs is the location of your image. – jewelsea Dec 27 '21 at 19:56
  • It is probably a good idea to have the icon available in different sizes and let the system choose the appropriate size, see [this example](https://stackoverflow.com/questions/27017031/javafx-2-window-icon-not-working). That is what [the documentation](https://openjfx.io/javadoc/17/javafx.graphics/javafx/stage/Stage.html#getIcons()) requests: "The images should be different sizes of the same image and the best size will be chosen, eg. 16x16, 32,32.". Square icons with powers of two sizes are best. – jewelsea Dec 27 '21 at 19:56

0 Answers0