File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes how do you load images from a jar file for image buttons? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how do you load images from a jar file for image buttons?" Watch "how do you load images from a jar file for image buttons?" New topic

how do you load images from a jar file for image buttons?

jack lista

Joined: Feb 05, 2004
Posts: 2
I have an application with a pretty simple swing GUI that uses a few image JButtons [which use the ImageIcon as the JButton constructor argument, etc.]. When I had the classes for this GUI loose on the file system and the images loose as well in the same directory from which I ran the code [i.e. the directory from which I ran "java" etc.] everything worked fine. Now I have jarred up the classes and included the images in the jar file and while the GUI runs fine, it seems it can't find the images in the jar as the buttons come up with out showing the image. I have tried putting the images in the jar at the top level, i.e. having no path, and also tried creating a directory in the jar[called "images"] and referenced the images in the images directory like this: "images/imagename.jpg" but the files are still not being found as the GUI comes up with no displayed images in the buttons. I seem to remember there was a trick to doing this but it's been a long time since I've done any swing GUI work and I can't remember how to do it for the life of me. The basics of the code looks something like this:
ImageIcon buttonImgIcon = loadImageIcon("starfield.gif","foo!");
JButton button = new JButton( buttonImgIcon );
pane.setLayout(new BorderLayout());
pane.add( button, "Center" );
frame.getContentPane().add(pane, BorderLayout.CENTER);
If you want to see the entire src code, it's only a single file and not all that huge, called I posted it as a reply to Craig Parsons' question on topic "running gifs", since he seems to be stuck getting images to show up at all. My code will load the images but only if they're loose on the file system, not if they're in the jar. I want to include them in the jar because if I don't they may get deleted or something and it's just generally easier to manage if it all is in a jar. Does anybody know how to load images from a jar for an application? All the code for the app is also in the jar and obviously the jar is on the classpath but the @&#$%$#$ images still won't load... duh-oh!
Thanks, in advance...
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
I use this for images in a jar:
My images are in the .jar under the /images directory, so I load them like so

Hope this helps!

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: how do you load images from a jar file for image buttons?
It's not a secret anymore!