Using a HashMap with a DropDownChoice component in Wicket

Lately, I’ve been mucking around with the DropDownChoice component in Wicket, mainly because I want to have separate id and display values in a dropdown choice. ie I want to render a label, eg “one”, “two”, “three”, but have Wicket see that as some id, eg 1,2,3. Simple stuff really.

At first glance I can see a HashMap being given in the constructor for a dropdown choice to be a really easy way to do this. Nope. All the documents and examples given seem to be for single dimensioned lists where the id and display values would be the same.

So after much frustration, I present the solution.

Firstly, wrap the HashMap with a Model and in its getObject method, return the keys of the HashMap (ie a List!). Then, in the DDC constructor, give it the model and use an IChoiceRenderer which will separate out the id from the display values and use them in the right spot.

final LinkedHashMap preferences = new LinkedHashMap();
preferences.put("0", "some choice");
preferences.put("1", "some other choice");
preferences.put("2", "and another choice");

IModel dropDownModel = new Model() {
  public Object getObject() {
    return new ArrayList(preferences.keySet());

DropDownChoice profileChoice = new DropDownChoice("preference", dropDownModel, new IChoiceRenderer() {
  public String getDisplayValue(Object object) {
    return preferences.get(object);
  public String getIdValue(Object object, int index) {
    return object.toString();

Note that in this example I haven’t given my DDC a Model for the user selected data to be stored in. This is because my wrapping form has a CompoundPropertyModel (a POJO) attached to it and the id of my DDC (‘preference’) maps to a field in that Model.

Also, to set a default value for the DDC, just set a value for the property in the model.

In this case:


will cause the DDC that is rendered to have the item that maps to ‘2’ selected already.

Not entirely Sakai related, but its code for a Sakai tool šŸ™‚


3 thoughts on “Using a HashMap with a DropDownChoice component in Wicket

  1. hey – sorry for a dumb question, but what object are you referencing when you set ‘preferencesModel.setPreference(2);’?. i’m assumimg this is an other data model object? Any clarrification would be appreciated.

  2. Hi Paul, sorry I only just noticed your comment. preferencesModel is the CompoundPropertyModel that backs my form. When you link up the form components Wicket will detect that it’s got a value and so assign the right default choice in the dropdown. Same for checkboxes, if the property is true, then it will be checked!

  3. dropdownchoice works on lists of objects. it is assumed that both id and display value can be derived from the object – this is what the choice renderer does.maps can be easily transformed into a list of such objects by simply calling .entryset() with each entry carrying the id and the you will need two things:1) a model to transform a hashmap into the listclass entrysetmodel extends abstractreadonlymodel { private final map map; public getobject() { return new arraylist(map.entryset());}}and a choice renderer that can render the key as id and value as display value:new choicerenderer(“value”,”key”)putting it all together:class mapdropdownchoice extends dropdownchoice { public mapdropdownchoice(string id, imodel model, map choices) { super(id); setmodel(model); setchoices(new entrysetmodel(choices)); setchoicerenderer(new choicerenderer(“value”, “key”);}}done. not that frustrating once you understand how things actually work šŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s