Skip to content

How to use EnumLazyIconProvider

Mikle edited this page Jan 30, 2020 · 4 revisions

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


What is it for?

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.


What should I know about it?

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

How to use it?

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.