Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse project with ImageIcon

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.

like image 834
user174084 Avatar asked Jul 27 '26 08:07

user174084


1 Answers

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

like image 169
virgium03 Avatar answered Jul 29 '26 21:07

virgium03



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!