views:

27

answers:

3

I have an eclipse project with image folder. I need to create an ImageIcon from an image located in this folder but can not figure out how to tell it to look in the folder with out giving exact position on my system which I do not want to do as the project gets moved around to different pc's.

Code example

ImageIcon ai = new ImageIcon("/somedir");

Thanks for the help.

A: 

You can fixed up the position. like home dir / appname

or you can make it configurable. put a properties file in specified location that will read the location of your image folder

org.life.java
+2  A: 

you have to store the image folder into your projectand then:

java.net.URL imageURL = this.getClass().getClassLoader().getResource("path_to_image_folder");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);

The path to the image folder can be relative to your class or absolute (starts with "/").

You can take a look here: How to use icons

virgium03
A: 

It's very easy (remove the backslash):

ImageIcon ai = new ImageIcon("somedir");
nanda