Solved

ImageIcons???

Posted on 1998-07-28
2
262 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now