A regular question on the Wicket mailinglist is how to pre-select a value in a drop down box.

If you look at the following select box, you see that I've pre-selected the value 'female', while the value 'male' is first in line.

Example code:



Gender: malefemale

Typically you'll select an object value from a list, or an enum value (Java 5) from the list. First, lets create the enum for Gender:

public enum Gender {
    male, female;

Now if we have a form that allows you to change the gender of a person using a drop down choice, this would typically look like the following example:

form.add(new DropDownChoice(
            new PropertyModel(person, "gender"), 

So how do we make sure that female is the default choice? As you can see, the component has a property model as its model, and it is bound to the gender of the person. By setting the gender of the person to 'female' the drop down choice box will select that value when it is rendered.


New users of the Wicket framework typically try to use an interaction based approach when working with components. This is typically done by setting a value on the component, and in the submit handler querying the component for the value. Often people use String values to put in the component and retrieve from, having to do type conversion and checking themselves. This circumvents a lot of basic features Wicket provides to you. If you use the model based approach as shown in the example above, you will get automatic type conversions, automatic setting of values and validation.