• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 org.foo.QuartzJobScheduler" 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 QuartzJobManager.java. 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...
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic