We help IT Professionals succeed at work.


stepma asked
Medium Priority
Last Modified: 2010-03-30
I have a GUI component which I created, and it has an icon with it.  I want to package the icon with the component so that it is all together.  In my class I have:  new ImageIcon("icons/myIcon.gif")

I also wrote a test program to test the component.
If I run the test program from the same package that the component is in, everything works fine, but if I put the test program outside of the component's package, the icons don't show up.  How can I fix this so that the icons will show up, but I do not want to hard code a path like C:\java\myStuff\gui\icons\myIcon.gif?
For some reason, the only way the icon shows up is if I run
Watch Question

The images filenames are taken from the path relative to current execution directory if you dont give absolute path. Rather, put in the properties file for your program, the path from where you want to load the images, read the path and attach image filename and load into image icon.

in fact, ImageIcon source calls Toolkit.getDefaultToolkit.getImage(filename) method to get the image object for the filename. Thus behavior is same as getImage method.

In case of applets, put it in getCodeBase() directory.

Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.