You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ralf Eichinger <ra...@pixotec.de> on 2010/08/17 01:04:08 UTC

How to render Enum values as RadioGroup and set default value

I have an User class with a gender field using a Gender-Enum:

public class User implements DomainObject<Long> {
...
  private Gender gender;

I want to bind this gender field to a radiogroup (representing all enum
values) in a registration form.
This is what I did so far:

<code>
public enum Gender {
	MALE, FEMALE
};

public class RegistrationEntryPanel extends Panel {
  private User user = new User();

  public RegistrationEntryPanel(String id) {
    super(id);

    // default value
    user.setGender(Gender.MALE);

    CompoundPropertyModel userModel = new CompoundPropertyModel(user);
    final RadioGroup<Gender> genderGroup = new RadioGroup<Gender>(
      "genderGroup", userModel.bind("gender"));

    Form form = new Form("form", userModel) {
      @Override
      protected void onSubmit() {
        System.out.println("USER: " + user);
      }
    };
    add(form);
    form.add(genderGroup);

    // gender
    Gender[] genderValues = Gender.values();
    List<Gender> genders = Arrays.asList(genderValues);

    ListView<Gender> genderList = new ListView<Gender>("genderList",
      genders) {
      @Override
      protected void populateItem(ListItem<Gender> item) {
        item.add(new Radio<Gender>("radio", item.getModel()));
        item.add(new Label("label", getString(item.getModelObject()
          .toString())));
      }
    };
    genderGroup.add(genderList);

  }
}
</code>

This works so far: I can select a radio button and the value in the user
object is changed on submit.
But as I have to use different models for the group and the listview,
the default setting with "user.setGender" has no effect.

How can I preselect the first radio button of the listview or better the
dedicated MALE radio button?


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to render Enum values as RadioGroup and set default value

Posted by datazuul <ra...@pixotec.de>.

Sven Meier wrote:
> 
> 
> datazuul wrote:
>> 
>> But as I have to use different models for the group and the listview,
>> the default setting with "user.setGender" has no effect.
>> 
> 
> Sure, the group has a model pointing to the user's gender, the listview a
> model holding all available genders.
> 
As ListView wants a List in the constructor to iterate over, I unfortunately
had to extract the enum values, which breaks the binding...


Sven Meier wrote:
> 
> Your code looks fine to me, userModel.bind("gender") works for setter and
> getter.
> 
I am using userModel.bind("gender") for the RadioGroup to bind it to
user.set/getGender and tried to set the default value with
user.setGender(Gender.MALE);. I expected some RadioGroup-inside-mechanism
that activates the Radio-button that has this value. But this does not
happen...
Maybe I have to implement a comparison inside populateItem to set the
Radio-button to "activated" when it's value is equal to the userModel's
value? But how to activate a Radio-button (should I really set the
HTML-attribute "checked" in Java-Code)?

Finally I just want to iterate over all values of an Enum and represent them
as Radio Buttons in a RadioGroup, setting one as default.
Not so unusual, should be solved by someone else before...
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-to-render-Enum-values-as-RadioGroup-and-set-default-value-tp2327596p2328095.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to render Enum values as RadioGroup and set default value

Posted by Sven Meier <sv...@meiers.net>.

datazuul wrote:
> 
> But as I have to use different models for the group and the listview,
> the default setting with "user.setGender" has no effect.
> 

Sure, the group has a model pointing to the user's gender, the listview a
model holding all available genders.

Your code looks fine to me, userModel.bind("gender") works for setter and
getter.

Sven
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-to-render-Enum-values-as-RadioGroup-and-set-default-value-tp2327596p2327890.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org