Reputation: 1107
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.
Upvotes: 3
Views: 4761
Reputation: 24788
It's very easy (remove the backslash):
ImageIcon ai = new ImageIcon("somedir");
Upvotes: 0
Reputation: 637
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
Upvotes: 4