Solved

ImageIcons???

Posted on 1998-07-28
2
273 Views
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
0
Comment
Question by:stepma
  • 2
2 Comments
 
LVL 4

Expert Comment

by:evijay
ID: 1228826
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.


0
 
LVL 4

Accepted Solution

by:
evijay earned 50 total points
ID: 1228827
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.


0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
how do i compare an object based on two fields 6 54
Basic Java Case or If-Else statement... 3 51
more than one jdk and one jre 1 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question