File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Enum with selectItems Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSF
Reply Bookmark "Enum with selectItems " Watch "Enum with selectItems " New topic
Author

Enum with selectItems

Ryan Slominski
Ranch Hand

Joined: Nov 05, 2010
Posts: 33
When working with an enum in a "pick an item from this list" kind of component such as a selectOneMenu it would be nice to not have to create a utility method to map the enum values to an array of SelectItems. When working with a List of objects you can take advantage of the var, itemLabel, and itemValue attributes of the selectItems component (and thus avoid the silly utility method). This doesn't seem to work with the array of values available with the enum.

Maybe the value attribute can only be a collection, not an array if you intend to iterate over the items using var, itemLabel, and itemValue?

I've read through this post: JSF-Populate-SelectOneRadio-Enum-values, but it makes no mention of the var, itemValue, or itemLabel attributes.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14568
    
    7

To make a selectItem list for an enum, you can build a list or array or SelectItem (enum.ordinal(), enum.name()). That's about as simple as you can get for a construct with no "get" methods.

Or actually, I think it's "SelectItem(enum, enum.name()).


Customer surveys are for companies who didn't pay proper attention to begin with.
Ryan Slominski
Ranch Hand

Joined: Nov 05, 2010
Posts: 33
If I am understanding your response correctly you are saying that there is no way to avoid a utility method with an enum?

I'm currently using a utility method, but I was wondering if there was a better way since there is a shortcut for a collection (var attribute).
 
jQuery in Action, 2nd edition
 
subject: Enum with selectItems
 
Similar Threads
dynamic selectOneRadio
Simple Example for Enums and selectOneRadio
JSF2 Populate SelectOneRadio with Enum values
selectoneRadio load with default value
selectOneRadio problem