1

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.

LastStar007
  • 725
  • 1
  • 8
  • 21
  • Could you add the source code where you instantiate the `ImageIcon`? – Eng.Fouad Jun 04 '12 at 05:44
  • to change 156th line in FrameView from setVisible(false) to setVisible(true), rest of changes I can't see from my magic globe (low baterry), for better help sooner post an [SSCCE](http://sscce.org/) – mKorbel Jun 04 '12 at 05:47
  • The NetBeans GUIBuilder makes its own source code, but here you go anyway: `jLabel1.setIcon(new javax.swing.ImageIcon("C:\\NoSpace\\Minesweeper\\Minesweeper graphics\\one.png"));` – LastStar007 Jun 04 '12 at 06:07
  • 1) Don't use a `File` to load an embedded resource. (WAG) 2) For better help sooner, post an [SSCCE](http://sscce.org/). (Excellent generic advice) – Andrew Thompson Jun 04 '12 at 06:15
  • @LastStar007 That is exceptionally poor advice, even for a comment. 1) Using a `String` that represents a `File` for an application Icon. 2) Using an absolute path for same. 3) Making it platform specific in the path separator. – Andrew Thompson Jun 04 '12 at 06:19
  • Have you tried creating a new package in your src folder, adding the icon there and then using "Image within project"? – predi Jun 04 '12 at 13:13
  • Yes. I've also tried importing the image to the same package as the JLabel, to no avail. – LastStar007 Jun 04 '12 at 13:21
  • @LastStar007 Did you try in an empty Form? I've reproduced your description and it works well! So if your Form is empty I agree there is a problem. If not the image doesn't show probably because there is a mistake elsewhere. If so consider posting an sscce. You can also try to use Clean and Build before running, maybe you have a pb with netbeans Ant script and he doesn't update itself to use the new image... To conclude I'll suggest to use "image within project or ""custom code" instead of "image chooser" and "external file". It will be better for portability. – alain.janinm Jun 06 '12 at 13:26
  • This is a nice tutorial -> http://netbeans.org/kb/docs/java/gui-image-display.html – alain.janinm Jun 06 '12 at 13:28

3 Answers3

1

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.

predi
  • 5,528
  • 32
  • 60
1

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.

traderjosh
  • 321
  • 5
  • 16
  • I'm having the same problem. Converted the folder to a package. Re-imported all the images. Works fine in the designer but fails to run with .getResource() unable to find the image file. – Walter ZAMBOTTI Feb 24 '21 at 02:46
1

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.