4

I have 6 JButtons on my GUI all have images on it, when I compile and run the code, all images on JButtons show up perfectly but in runnable JAR file, images on JButtons are not showing up.. how do I fix this problem?

I used this method in my code to show icons on JButtons

ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
jb1 = new JButton(SettingsIc);
jb1.setFocusPainted( false );
//jb1.setBorderPainted(false); 
jb1.setContentAreaFilled(false);

This is how my GUI looks when I compile my code in Eclipse enter image description here

This is how my GUI looks after executing Runnable JAR file enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Aditya Ponkshe
  • 3,840
  • 4
  • 39
  • 58
  • 4
    You're likely trying to access the images as files and files don't exist in jar files. Instead try to access them as resources. This has been asked many times on this site and others (including several answers by me), and you can easily find more on this with a little searching. Give it a try and you won't be disappointed. Good luck. – Hovercraft Full Of Eels Jan 30 '13 at 04:04
  • 3
    are those icons are in correct path? in jar file? ie under bin/images/settings.png? – Pradeep Simha Jan 30 '13 at 04:04
  • 2
    @HovercraftFullOfEels I think you have 'hit the nail on the head' yet again. As to the 'likely' note that `new ImageIcon("bin/images/settings.png");` supports the `File` part of it and the name vaguely suggests an application resource that *should be* an [tag:embedded-resource]. – Andrew Thompson Jan 30 '13 at 04:12
  • @user2003602 : Have a look at this [answer](http://stackoverflow.com/a/9866659/1057230) – nIcE cOw Jan 30 '13 at 11:39

2 Answers2

5

This (as pointed out by a number of people)

ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");

Suggests that you are trying to load the images from the bin/images off the file systems. This is a relative path from the execution point of your application.

ImageIcon won't complain if the file does not exist.

If possible, you are better off embedding the resources within your Jar file (it will make it easier to deploy) and use something like getClass().getResource("/bin/images/settings.png") to load the images.

If possible, you should try using ImageIO.read(URL) to load your images, it will throw an exception if the resource pointed to by the File/URL does not exist (or is invalid).

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • thanks for the answer i will certainly try the method you mentioned. But i noticed that if I unzip my jar file there is a folder called images in it, is it possible to access that folder? – Aditya Ponkshe Jan 30 '13 at 04:20
  • 1
    Yes. I imagin it would be `/images/settings.png` instead of `/bin/images/settings.png`, but it would depend on the directory structure – MadProgrammer Jan 30 '13 at 04:21
  • 1
    @user2003602: again it depends on your jar directory structure, something we don't know well. MadProgrammer's advice is spot on. 1+ – Hovercraft Full Of Eels Jan 30 '13 at 04:25
  • 2
    @user2003602 Also - make sure you're using `getClass().getResouce(...)` not just the plain string value. A `String` value indicates a file resource, an embedded resource is not a file. – MadProgrammer Jan 30 '13 at 04:29
-2

Just keep the jar and images in the same folder and keep

ImageIcon icon = new ImageIcon("image.jpg");

in the code

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ashwin
  • 1