Solved

ImageIcons???

Posted on 1998-07-28
2
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

730 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