1

I'm trying to add an Icon to my JavaFX 2 application, but the ways I have found don't seem to work.

Image icon = new Image(getClass().getResourceAsStream("/images/icon.png"));
stage.getIcons().add(icon);

The icon is 32x32 in size.

When I try

Image icon = new Image("http://goo.gl/kYEQl");

It does work, in Netbeans and in the runnable jar.

I hope this can be fixed.

Erates
  • 646
  • 1
  • 9
  • 24
  • Are you sure, your image is being loaded? – SSC Nov 19 '14 at 12:42
  • If I understand the documentation correctly, the resource stream is superfluous, and you can use the filepath directly. Anyways, make sure that the image exists in the location you have given again.. – Vogel612 Nov 19 '14 at 12:45
  • Yes, because when I remove the "/" in front of "images" or use "/images/icon2.png" I get a NullPointerException refering that the Inputstream cannot be null. – Erates Nov 19 '14 at 12:45
  • http://stackoverflow.com/questions/20094620/set-icon-on-stage-in-javafx/20094784#20094784 – subash Nov 19 '14 at 12:56
  • @subash I've tried that, but it doesn't seem to display the icon. – Erates Nov 19 '14 at 12:58

1 Answers1

8

The problem was in the icon itself. It did load it like it should, but for some reason it didn't display like it should.

I remade the icon I was trying to use to different sizes (16x16 up to 512x512) and added them all to the icon list.

stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_16.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_32.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_64.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_128.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_256.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_512.png")));

Now it uses the icon like it should.

Erates
  • 646
  • 1
  • 9
  • 24