Set Icon on Stage in JavaFX

38,281

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.

Share:
38,281

Related videos on Youtube

Siavash
Author by

Siavash

Updated on July 18, 2021

Comments

  • Siavash
    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.

  • Siavash
    Siavash over 10 years
    Thank you very much, using this method no exception will be thrown, but the image icon of stage has not changed!!
  • Siavash
    Siavash over 10 years
    Thank you very much, using this method no exception will be thrown, but the image icon of stage has not changed!!
  • Siavash
    Siavash over 10 years
    Thank you very much, using this method no exception will be thrown, but the image icon of stage has not changed!!
  • Siavash
    Siavash over 10 years
    Thank you again, but it does not work with this path: file:images/pic.jpeg
  • Maher Abuthraa
    Maher Abuthraa over 10 years
    try to clean project and build it again not only run.
  • subash
    subash over 10 years
    if you use getResourceAsStream("") no need to file: . it is just a protocal. so remove the file: if you are using getResourceAsStream
  • Siavash
    Siavash over 10 years
    Did as you said, but I receive this exception: Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Input stream must not be null
  • isnot2bad
    isnot2bad over 10 years
    It 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
    Siavash over 10 years
    It 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
    Siavash over 10 years
    stage.getIcons().add(new Image(getClass().getResourceAsStream("bal.png"))); stage.getIcons().add(new Image(getClass().getResourceAsStream("bal2.png")));
  • subash
    subash over 10 years
    look my answer. upload my directory structure. where .java file located there your image file locate.
  • Jitendra Pareek
    Jitendra Pareek over 10 years
    Yes 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
    Siavash over 10 years
    I am using Java 8 on eclipse Kepler.
  • Siavash
    Siavash over 10 years
    Thank 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
    subash over 10 years
    then where you want to put??
  • Siavash
    Siavash over 10 years
    Because 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
    Siavash over 10 years
    But 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
    subash over 10 years
    can you post your directory structure..?
  • ferrerverck
    ferrerverck almost 10 years
    Actually 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
    APC over 8 years
    This 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
    manikant gautam over 8 years
    I thought it would be helpful by the help of example.Instead of putting a single line code.

Related