How to display an enum in a dropdown in Wicket

      No Comments on How to display an enum in a dropdown in Wicket

Its often useful to use an enum as the model for a dropdown (or radio group) in Wicket, but not immediately obvious how to handle the display of the values. Lets say I have an enum with three colors:

[sourcecode language=”java”]
enum Color { RED, GREEN, BLUE };
[/sourcecode]

I can hook this up to a dropdown with the following code:

[sourcecode language=”html”]

[/sourcecode]

[sourcecode language=”java”]
enum Color { RED, GREEN, BLUE };
private Color color = Color.GREEN;
add(new DropDownChoice(“color”, Arrays.asList(Color.values()), new EnumChoiceRenderer(this)));
[/sourcecode]

[sourcecode language=”java”]
Color.RED=Red
Color.GREEN=Green
Color.BLUE=Blue
[/sourcecode]

Now I get what I wanted – a dropdown displaying “Red”, “Green” and “Blue”.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.