IconsFactory
provides a consistent way to access icon resource in any application.
Any application usually need to access image files. One way to do it is to put those image files in the installation
and access them use direct file access. However this is not a good way because you have to know the full path to the
image file. So a better way that most Java applications take is to bundle the image files with in the jar and use
class loader to load them.
For example, if a class Foo needs to access image files foo.gif and bar.png, we put the image files right below the
source code under icons subfolder. See an example directory structure below.
/src/com/jidesoft/Foo.java
/icons/foo.gif
/icons/bar.png
When you compile the java class, you copy those images to class output directory like this.
/classes/com/jidesoft/Foo.class
/icons/foo.gif
/icons/bar.png
Notes:
In IntelliJ IDEA's "Compile" tab of "Project Property" dialog, there is a way to set "Resource Pattern".
Here is the setting on my computer - "?*.properties;?*.xml;?*.html;?*.tree;?*.gif;?*.png;?*.jpeg;?*.jpg;?*.vm;?*.xsd;?*.ilayout;?*.gz;?*.txt"
for your reference. If so, all your images will get copies automatically to class output folder. Although I haven't
tried, I believe most Java IDEs have the same or similar feature. This feature will make the usage of IconsFactory
much easier.
If you setup directory structure as above, you can now use IconsFactory to access the images like this.
ImageIcon icon = IconsFactory.get(Foo.class, "icons/foo.gif");
IconsFactory will cache the icon for you. So next time if you get the same icon, it will get from cache instead of
reading from disk again.
There are a few methods on IconsFactory to create difference variation from the original icon. For example,
#getDisabledImageIcon(Class,String) will get the image icon with disabled effect.
We also suggest you to use the template below to create a number of IconsFactory classes in your application. The
idea is that you should have one for each functional area so that all your image files can be grouped into each
functional area. All images used in that functional area should be put under the folder where this IconsFactory is.
Here is an template.
class TemplateIconsFactory {
public static class Group1 {
public static final String IMAGE1 = "icons/image11.png";
public static final String IMAGE2 = "icons/image12.png";
public static final String IMAGE3 = "icons/image13.png";
}
public static class Group2 {
public static final String IMAGE1 = "icons/image21.png";
public static final String IMAGE2 = "icons/image22.png";
public static final String IMAGE3 = "icons/image23.png";
}
public static ImageIcon getImageIcon(String name) {
if (name != null)
return IconsFactory.getImageIcon(TemplateIconsFactory.class, name);
else
return null;
}
public static void main(String[] argv) {
IconsFactory.generateHTML(TemplateIconsFactory.class);
}
}
In your own IconsFactory, you can further divide images into different groups. The example above has two groups.
There is also a convenient method getImageIcon() which takes just the icon name.
In the template, we defined the image names as constants. When you have a lot of images, it's hard to remember all of
them when writing code. If using the IconsFactory above, you can use
ImageIcon icon = TemplateIconsFactory.getImageIcon(TemplateIconsFactory.Group1.IMAGE1);
without saying the actual image file name. With the help of intelli-sense (or code completion) feature in most Java
IDE, you will find it is much easier to find the icons you want. You can refer to JIDE Components Developer Guide to
see a screenshot of what it looks like in IntelliJ IDEA.
You probably also notice this is a main() method in this template. You can run it. When you run, you will see a
message printed out like this.
"File is generated at "... some directory ...\com.jidesoft.icons.TemplateIconsFactory.html".
Please copy it to the same directory as TemplateIconsFactory.java"
if you follow the instruction and copy the html file to the same location as the source code and open the html, you
will see the all image files defined in this IconsFactory are listed nicely in the page.
By default, all image files are loaded using ImageIO. However if you set system property "jide.useImageIO" to
"false", we will disable the usage of ImageIO and use Toolkit.getDefaultToolkit().createImage method to create the
image file.