0

I'm trying to load a single JLabel set with an ImageIcon for display. It works fine when I compile it within my IDE, but when I export the image will not show up. Here's the code I'm using to set the image.

ImageIcon icon = new ImageIcon("test.png");
JLabel label = new JLabel(icon);

I know I asked a question along the same lines as this last time, so I tried "getClass().getResource(...)", but that's been throwing an error at me. What do I need to do with this JLabel to get that image to show even when I export? I'd like the image to be part of the JAR package, as this entire thing is supposed to be something of a surprise.

Chiubaka
  • 801
  • 2
  • 11
  • 27

1 Answers1

0

Most likely test.png is not in the classpath of your application. Assuming it is a jar, the image may be in a subfolder, say, image, in which case, you would need to specify the following

ImageIcon icon = new ImageIcon("image/test.png");

If this is not the case, please edit the question and specify the layout of the jar - how the contents, especially the images are organized therein.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • So within my project folder I have a folder named "src" and a folder named "img". The image I want to use is located within img, all the source classes are located in "src". I'm finding that "test.png" and "img/test.png" work interchangeably in my IDE, but neither works in the exported JAR. – Chiubaka Feb 06 '11 at 08:53
  • that is incorrect. new ImageIcon("..") loads from a _file_, not the classpath. – Thorbjørn Ravn Andersen Feb 06 '11 at 10:26