I have two questions for Icons.
I am using ImageIcons as default Icons on JTree. As suggested by tutorials I am loading icons using this method :
protected ImageIcon createImageIcon(String path) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
Although in order for this to work I have to store the images inside the file with the source code which I believe is messy (but I read its needed for running on .jar). Is there a way to load images similarly while they are on the project folder and not on src ?
Also is there a way to automatically scale images on the correct size for displaying on JTree? Thanks !
The code doesn’t load the ImageIcon from the
srcfolder. It loads it from the runtime classpath. And for the images to be in the runtime classpath, the easiest way is indeed to place them in thesrcfolder, so that your IDE automatically copies them to the target folder, along with the .class files.If you want, you may define another source directory (named
resources, for example) in your project, and the IDE will also copy the files of this source directory to the target folder. This allows separating Java files from resource files.