Search code examples
javaimageswingjframeembedded-resource

Program icon not showing up after Clean and Build (Netbeans)


I'm new at Java and also swing. I just created a small app using JFrame and added some buttons and textFields, also I have a method which set the icon that I want for the taskbar and the one in the left corner.

When I run the program in Netbeans everything seems correctly, but when I build the project the icon it's not showing up. I tried a lot of things but none of them worked for me.

here's the method that I use for the program:

    private void setIcon() {
        ImageIcon imageIcon = new ImageIcon("src/main/java/icons/steam.png");
        this.setIconImage(imageIcon.getImage());
    }

And I call the method from the constructor.

Thank you.

EDIT 1:

Implementing what Andrew said, now I have this:

BufferedImage img = null;
        try {
            URL url = getClass().getResource("src/main/java/icons/steam.png");
            img = ImageIO.read(url);
        } catch (IOException e) {
        }
        this.setIconImage(img);

And that's on the constructor. But when I run it I get:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1400)

I tried different paths but I can't get it. And yes, I'm sure that "steam.png" is there.


Solution

  • I been looking for a solution to this and I finally get it (thanks to Andrew trough the comments)

    First I edited my code as you can see in EDIT 1

    After that, I got an IllegalArgumentException and the problem was that I didn't have a "resources" folder under /src

    So I created my resources folder under src/main/resources and put my image inside

    Then I got it using

    URL url = getClass().getResource("/icons/steam.png");
    img = ImageIO.read(url);
    

    And that was the fix for my problem, now when I run the program images are now loaded.

    Thank you so much!