• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing: Using icon resources in a jar

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Pat!
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic