This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes putting image files to Jar File in Swing application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "putting image files to Jar File in Swing application" Watch "putting image files to Jar File in Swing application" New topic
Author

putting image files to Jar File in Swing application

Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Hi guys, I have a bit problem with my image in my GUI project.

I have a method to retrieve ImageIcon like this:

public static ImageIcon createImageIcon(Class sourceClass, Stringpath) {
URL imageURL = sourceClass.getClass().getResource(path);

if (imageURL != null) {
return new ImageIcon(imageURL);
} else {
System.err.println("Couldn't find file : + path");
return null;
}
}

Where should I put the image files in JAR file assuming all my images is in the directory named images?


Thanks for your answer

Jeffry Kristianto Yanuar
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Originally posted by Jeffry Kristianto Yanuar:
URL imageURL = sourceClass.getClass().getResource(path);

Since sourceClass is already a Class object, sourceClass.getClass will return Class.class. Just remove the getClass() call.

Where should I put the image files in JAR file assuming all my images is in the directory named images?

If the images directory is put in the root of the JAR file, you can access them by using "/images/myimage.gif" for the path. If the images directory is in the same folder as the class file, just remove the leading /


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Thanks for your reply, it works!!!


Jeffry Kristianto Yanuar
 
GeeCON Prague 2014
 
subject: putting image files to Jar File in Swing application