8

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.

rotsch
  • 1,909
  • 16
  • 36
  • I just found the problem: I was using `AjaxEventBehavior` on my `RadioGroup` instead of `AjaxFormChoiceComponentUpdatingBehavior`. This fixed the model updating for my code in the question. – rotsch May 10 '11 at 14:23
  • Good to hear you've found the error.. It wasn't apparent from your question that you were using Ajax at first, but yes the AFCCUB is a requirement with choice components in Wicket. – Tim May 10 '11 at 14:52

2 Answers2

2

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..

Tim
  • 19,793
  • 8
  • 70
  • 95
  • thanks, but how do I achieve this with radio buttons? (see edit in my post) – rotsch May 10 '11 at 11:11
  • @rotsch I'm not a 100% sure, but look into using [RadioChoice](http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/form/RadioChoice.html) rather than plain [Radio](http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/form/Radio.html), since you're already using the RadioGroup.. – Tim May 10 '11 at 11:37
  • RadioChoice is to unflexible, but thanks anyway, I found the error, see top – rotsch May 10 '11 at 14:18
2

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.

rotsch
  • 1,909
  • 16
  • 36