Eclipse Project with ImageIcon

I have an eclipse project with an image folder. I need to create ImageIcon from an image located in this folder, but I can’t understand how to tell it to look in the folder without indicating the exact position in my system, which I don’t want to do when the project is moving to different PCs.

Code example

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

Thanks for the help.

+3
source share
2 answers

you need to save the images folder to your project, and 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 look here: How to use icons

+3
source

( ):

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

Source: https://habr.com/ru/post/1769110/


All Articles