All gui swing classes uses the IconBundle class to get there icons. This class is managing ImageIcons to avoid having two times the same image loaded in memory, if the image has allready been loaded it will return you the same reference.

Isn't it a little heavy to load images and images in memory like this?
Don't worry, this class use weakreferences for ImageIcons, so they are remove by the Garbage Collector when no one uses them.

I consider icons as not important and should never block the program, that's why the IconBundle class will never return errors, in the worst case (can not fine the image) it will return a 1x1 transparant ImageIcon.

Changing/adding icons

make a property file :


Once the file is ready, load it once at startup in the IconBundle :

ResourceBundle bundle =  ResourceBundle.getBundle( path_to_new_bundle );
IconBundle.getResource().setBundle( bundle );

Getting an icon

nothing simpler :

ImageIcon img = IconBundle.getResource().getIcon("maplayer_visible");