-
Notifications
You must be signed in to change notification settings - Fork 235
How to use EnumLazyIconProvider
Available since WebLaF v1.27 release, updated for WebLaF v1.2.12 release
Requires weblaf-core module and Java 6 update 30 or any later to run
After creating over a thousand different enumerations and icons attached to each enumeration I have decided to simplify that process and dramatically reduce the amount of code required to attach icons to enumerations. This is when I have created EnumLazyIconProvider
class that has just a few powerful methods.
There are a few simple things you should know:
- EnumLazyIconProvider is actually lazy and loads specific icon only when it is requested
- EnumLazyIconProvider only loads icons with locations relative to the enumeration class
Imagine you have an enumeration you want to attach usable icons to:
public enum MyEnum
{
floor,
table,
chair
}
Note that I have specified class package, that is important!
And you have appropriately-named icons somewhere around that class, for example in a separate package "icons/home/", so the final structure looks like:
- com/my/test/MyEnum.java
- com/my/test/icons/home/floor.png
- com/my/test/icons/home/table.png
- com/my/test/icons/home/chair.png
Here is what you have to do to attach icons to each separate enum:
public enum MyEnum
{
floor,
table,
chair;
public ImageIcon getIcon ()
{
return EnumLazyIconProvider.getIcon ( this, "icons/home/" );
}
}
And this is how you can use those icons:
final ImageIcon icon = MyEnum.floor.getIcon ();
You can also provide state-dependant icons:
public enum MyEnum
{
floor,
table,
chair;
public ImageIcon getIcon ( final String state )
{
return EnumLazyIconProvider.getIcon ( this, state, "icons/home/" );
}
}
This is how icons for example states open
and close
should look like:
- com/my/test/MyEnum.java
- com/my/test/icons/home/floor-open.png
- com/my/test/icons/home/floor-close.png
- com/my/test/icons/home/table-open.png
- com/my/test/icons/home/table-close.png
- com/my/test/icons/home/chair-open.png
- com/my/test/icons/home/chair-close.png
In later updates you will also be able to provide icon extension.
If you found any mistakes or inconsistency in this article, feel that it is lacking explanation or simply want to request an additional wiki article covering some topic:
I will do my best to answer and provide assistance as soon as possible!