Netbeans ImageIcon not displaying

9.9k views Asked by At

I am using the NetBeans GUIBuilder to make a JPanel Form. I added a JLabel and used NetBeans' interface to give it an icon from an external image (.png). The path is verified and the image shows up on the GUIBuilder screen. It even shows up when I click the "Preview Design" button. It DOES NOT show up when I RUN the project. The rest of the GUI appears as it should. Do any of you know why this happening and/or how to fix it?

A lot of you have been asking for an SSCCE. Since the code is generated by the NetBeans Form Builder, I have instead included the steps I took to make the JLabel. The areas of focus are circled in red.

  1. Drag and drop a JLabel into the Form Builder. Drag and drop a JLabel into the Form Builder.

  2. Open up the JLabel's properties menu. Enter the empty string ("") for the text field. Click the ellipsis next to icon. Open up the JLabel's properties menu. Click the ellipsis next to icon.

  3. Select External Image and click the ellipsis. Select External Image and click the ellipsis.

  4. Select the image of choice. In my case it's a .png. Select the image of choice.

  5. Notice that the image appears in the icon preview. Notice that the image appears in the icon preview.

  6. Close the icon menu and the properties menu, and notice that the image appears as the JLabel's icon on the Form Builder. Close the icon menu and the properties menu, and notice that the image appears on the Form Builder.

Thank you for accepting an unorthodox SSCCE and thank you in advance for your help.

3

There are 3 answers

0
predi On

I found out the hard way that relying on Netbeans GUI builder to do everything for you is a mistake.

Just create an icon fetching class like the one below, put the icons in it's package, and use "Custom code" instead of "Image chooser". Sure the icons will not be visible inside NB. But if they show up when the app is running, who cares about that.

package com.example.resource.icons;

import javax.swing.ImageIcon;

public class IconFetch {

    private static IconFetch instance;

    private IconFetch(){
    }

    public static IconFetch getInstance() {
        if (instance == null)
            instance = new IconFetch();
        return instance;
    }

    public ImageIcon getIcon(String iconName) {
        java.net.URL imgUrl = getClass().getResource(iconName);
        if (imgUrl != null) {
            return new ImageIcon(imgUrl);
        } else {
            throw new IllegalArgumentException("This icon file does not exist");
        }
    }

    public static final String MINESWEEPER_ONE = "one.png";
}

Usage:

IconFetch.getInstance().getIcon(IconFetch.MINESWEEPER_ONE);

If the icon still doesn't show up after trying this, then something might be wrong with the way you layed out components in your form (the label is there but you can't see it).

Hope this helps even though it's a long shot.

0
Lorenzo Prempratya Premvijit On

I do have a same problem also. But I found the solution.

  1. I create the package in project and put the images inside there.
  2. When I build the project, Netbeans will create 'target' folder and build .class files.
  3. I found that the images that I copied to the package, did not transfer to the 'target' folder.

Interim solution. 4. I copy all image to target folder with the same structure. Then I can run the project directly from Netbeans. 5. Incase you clean the project. Do no.4 again.

1
focus1691 On

I had the same problem, and predi's solution wasn't working either. Then I created a package instead of a folder, and added the images there, and it works now.