hc0re
hc0re

Reputation: 1986

JavaFX: stage.getIcons().add() not working in a JavaFX app

I want to add a program icon for my JavaFX app. I know how to do it - I have a Stage object defined as a field in the main app class:

private Stage stage;

And then in the method that is initilaizing the main stage I have a piece of code:

Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.getIcons().add(new Image("file:view/images/programicon.png"));
primaryStage.show();

I have a package com.myapp.view, and in this package I have another package: com.myapp.view.images, and I have pasted the icon I wanted to use, called programicon.png. Earlier, I had written it wrong:

primaryStage.getIcons().add(new Image("view/images/programicon.png"));

And I got an exception:

Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1110)

But when I changed the url to file:view/images/programicon.png - there is no exception now, but the program icon still remains default.

The programicon.png is a 512x512 image without background.

Upvotes: 0

Views: 962

Answers (1)

hc0re
hc0re

Reputation: 1986

I have found a solution. Changing from:

primaryStage.getIcons().add(new Image("file:view/images/programicon.png"));

To

primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("view/images/programicon.png")));

did the trick.

Upvotes: 2

Related Questions