Enum-backed OptionGroup subclass offered


I built a subclass of OptionGroup backed by an Enum, called
. My goal was to simply pass your enum, and its collection of instances would be used to populate the radio buttons of an OptionGroup. I wanted an one-liner, and succeeded. Here’s an example usage assuming an enum named DogBreed.

OptionGroup optionGroup = new EnumBackedOptionGroup<DogBreed>( "Choose Breed:" , DogBreed.class , DogBreed :: getTitle );

Note how we get type-safety by use of Generics.

The new Lambda syntax in Java 8 and later lets us pass a method reference to be called to determine the text labeling to be used for each option’s presentation. If you have not yet added such a method, omit that argument and the class falls back to calling toString. You need not have mastered Lambda yet; just follow the pattern to call this class.

I would appreciate feedback and criticism. Find the source code pasted at the top of
my Answer
on StackOverflow.com.

Viritin – EnumSelect

By the way, the famous
add-on for Vaadin offers a similar class. I was not able to make that work for me, apparently due to
my own confusion
. I don’t know exactly how Viritin’s class compares to my new class. I suspect my class might be useful when you want only this one simple feature of passing an enum to build a set of radio buttons without all the many other features of Viritin.