my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Swing: Using icon resources in a jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing: Using icon resources in a jar" Watch "Swing: Using icon resources in a jar" New topic
Author

Swing: Using icon resources in a jar

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
I am using icons on buttons. I included them in the jar file under jar/monthlyupdate.jpg

Then I bind the icon to the button in the constructor

jButtonRatesUpdate.setIcon(new ImageIcon("./icons/monthlyupdate.jpg"));

It still looks on the file system for the icon. I want it to look for the icon in the jar file.
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
Hi M Burke

The code is here
---------------
ImageIcon imageicon = new ImageIcon(getClass().getResource("/icons/monthlyupdate.jpg"));
jButtonRatesUpdate.setIcon(imageicon);


Download Java GUI Builder, <a href="http://www.mars3000.com" target="_blank" rel="nofollow">http://www.mars3000.com</a>
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
Thank you, Pat!
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
TIP:

We also usually put icons in a resources/images folder within the jar. Using this structure helps organize other types of resources and to identify quickly why a file is included in the jar...


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The Class.getResource() method in Pat's code above is an instance method. This means that you need a Class object to call it. Alternatively, you can use the static ClassLoader.getSystemResource() method. I'm not sure of the differences between these to methods, so you should probably read the javadocs to see if there are any that will impact your development.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Layne Lund:
The Class.getResource() method in Pat's code above is an instance method. This means that you need a Class object to call it. Alternatively, you can use the static ClassLoader.getSystemResource() method. I'm not sure of the differences between these to methods, so you should probably read the javadocs to see if there are any that will impact your development.


Layne,
If I am not wrong, I guess ClassLoader class is avaiable in the system, where custom class loaders is permitted... In limited systems like in J2ME, we need to use getClass().getResource() method in Pat's way to get into the resources in the jar...

But in desktop application and web application, where custom class loader is present, it won't be a big deal to use ur way or Pat's way, I guess...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
The single small difference is the way the name of the resource is resolved. Afterwards Class.getResources delegates to either ClassLoader.getSystemResource or ClassLoader.getResource.

--
./pope
[the_mindstorm]


blog - InfoQ.com
Zak Nixon
Ranch Hand

Joined: Sep 27, 2003
Posts: 126
Thanks everyone on this post for helping me solve this exact problem.
Javaranch is such a valuable resource!!



Zak Nixon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing: Using icon resources in a jar