• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Swing: Using icon resources in a jar

 
Ranch Hand
Posts: 428
  • 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: 428
  • 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: 805
  • 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
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic