Set Icon on Stage in JavaFX
Solution 1
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
Solution 2
The constructors of javafx.scene.image.Image
expect a URI, not a (full) path. This URI can either be relative (e.g. /images/flower.png
) or absolute (e.g. file:flower.png
).
Strings like G:/test.jpg
are no valid URLs and hence illegal.
Try file:g:/test.jpg
instead.
Usually, the icons should be bundled with your application, so simply put the image file into your classpath (e.g. if you're using eclipse, put it into your 'src' directory) and use it like that:
stage.getIcons().add(new Image("/logo.jpg"));
Solution 3
use
stage.getIcons().add(new Image(("file:logo.png")));
and put the image logo.png in root of your project ( at same directory where src )
Solution 4
Best Way:
stage.getIcons().add(new Image(getClass().getResource(IconImagePath).toExternalForm()));
Solution 5
don't forget that your icon must be in 32x32
or 16x16
resolution, if not, it doesn't work.
Related videos on Youtube
Siavash
Updated on July 18, 2021Comments
-
Siavash almost 3 years
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.
-
mavroprovato over 10 years
G:/test.jpg
is not a URL, what if you try withfile:/G:/test.jpg
? -
Michael Berry over 10 years
-
-
Siavash over 10 yearsThank you very much, using this method no exception will be thrown, but the image icon of stage has not changed!!
-
Siavash over 10 yearsThank you very much, using this method no exception will be thrown, but the image icon of stage has not changed!!
-
Siavash over 10 yearsThank you very much, using this method no exception will be thrown, but the image icon of stage has not changed!!
-
Siavash over 10 yearsThank you again, but it does not work with this path: file:images/pic.jpeg
-
Maher Abuthraa over 10 yearstry to clean project and build it again not only run.
-
subash over 10 yearsif you use getResourceAsStream("") no need to file: . it is just a protocal. so remove the file: if you are using getResourceAsStream
-
Siavash over 10 yearsDid as you said, but I receive this exception: Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Input stream must not be null
-
isnot2bad over 10 yearsIt you don't get any exception, it means that the image could be found and loaded. You should see it in the trimming area of your window, the windows task bar and the task list if you press Alt+Tab.
-
Siavash over 10 yearsIt was jpeg, I changed it to jpg, but it still is not working. What is this list of images on stage (stage.getIcons()), and what happens if someone adds to Image or more to it? I mean
-
Siavash over 10 yearsstage.getIcons().add(new Image(getClass().getResourceAsStream("bal.png"))); stage.getIcons().add(new Image(getClass().getResourceAsStream("bal2.png")));
-
subash over 10 yearslook my answer. upload my directory structure. where .java file located there your image file locate.
-
Jitendra Pareek over 10 yearsYes Subash Your answer is correct its working fine. May be Siavash have some version specific problem or he has not clean and build project. I am agreed its working.
-
Siavash over 10 yearsI am using Java 8 on eclipse Kepler.
-
Siavash over 10 yearsThank you in advance, I checked your solution and it worked, but I do not want to put my image file next to my .java files.
-
subash over 10 yearsthen where you want to put??
-
Siavash over 10 yearsBecause I have a lot of images, I have created a folder named images in the root of the project next to src and bin folders.
-
Siavash over 10 yearsBut it does not work!!! when I change the String "temp.jpeg" to "images/temp.jpeg" I get a null pointer exception which says inputstream must not be null.
-
subash over 10 yearscan you post your directory structure..?
-
ferrerverck almost 10 yearsActually it should work, but it doesn't for me. I am running Ubuntu and got the same issue with Swing. I think it is somehow connected to the application icon cache. And I still don't know how to fix it. Clean and Build doesn't help.
-
APC over 8 yearsThis is a little terse. Please explain why this solution is the "best way", especially when there are so many other answers posted already.
-
manikant gautam over 8 yearsI thought it would be helpful by the help of example.Instead of putting a single line code.