Set Icon on Stage in JavaFX


Question

I wanted to know how should I set icons on javaFX stage. I have found this method, but it did not work properly.

  stage.getIcons().add(new Image(iconImagePath));

stage is an instance of javafx.stage.Stage, and I have imported javafx.scene.image.Image. This is the exception which we receive:

Invalid URL: Invalid URL or resource not found

Also, there is nothing wrong with the iconImagePath, its value is "G:/test.jpg" and there is a jpg file in the G drive named test. In addition, when we use ImageIO to read the same URL we can do it easily.

1
8
11/20/2013 11:25:28 AM

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

This example works. I placed an icon in the same folder/package as the source .java file.

Directory structure

16
10/21/2015 2:18:18 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon