Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using enums as model object in Wicket

Tags:

java

enums

wicket

To use an Enum class in a PropertyModel you can write:

new PropertyModel(MyObject, "MyEnumClass");

Now this only works if MyEnumClass is defined within the MyObject-class.

How can I use a stand-alone Enum-class in a model?

Edit: I concretize:

RadioGroup<MyEnum> rg = new RadioGroup<MyEnum>("radioGroupID", new Model<MyEnum>(MyEnum.NORMAL));

rg.add(new Radio<MyEnum>("radioNormal", new Model<MyEnum>(MyEnum.NORMAL)));
rg.add(new Radio<MyEnum>("radioSpecial", new Model<MyEnum>(MyEnum.SPECIAL)));

The problem here is that changing the radio button doesn't change the model on the RadioGroup.

like image 956
rotsch Avatar asked May 10 '11 08:05

rotsch


2 Answers

I've been using the following without a problem for my Enum "NMRType" DropDownChoice component:

IModel<NMRType> default = Model.of(NMRType.HNMR);
List<NMRType> choices = Arrays.asList(NMRType.values());
DropDownChoice<NMRType> nmrDDC = 
    new DropDownChoice<NMRType>("nmrType", default, choices);

Just a note: Be careful not to write to your Enum models.. Wicket uses reflection, which might throw up a few surprises if you do..

like image 93
Tim Avatar answered Sep 30 '22 13:09

Tim


I just found the problem: I was using AjaxEventBehavior on my RadioGroup instead of AjaxFormChoiceComponentUpdatingBehavior.

This fixed the model updating problem for my code in the question.

like image 30
rotsch Avatar answered Sep 30 '22 13:09

rotsch