Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing: Using icon resources in a jar

 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi M Burke

The code is here
---------------
ImageIcon imageicon = new ImageIcon(getClass().getResource("/icons/monthlyupdate.jpg"));
jButtonRatesUpdate.setIcon(imageicon);
 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Pat!
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Zak Nixon
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone on this post for helping me solve this exact problem.
Javaranch is such a valuable resource!!



Zak Nixon
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic