Java – Multiple TextFields in a Wicket form

Multiple TextFields in a Wicket form… here is a solution to the problem.

Multiple TextFields in a Wicket form

I

have a bean that I attach to the form using a model and it works fine. I also have a field in the bean like Map<String, javax.mail.Address> Considering that every map entry should look like Label: TextField, how do I bind this field to a form via a model?

Thanks in advance.

Solution

Maybe something like this:

ListView<String> textAreasListView = new ListView<String>("someid", bean.map.keySet()) {
    @Override
    protected void populateItem(final ListItem<String> itemLang) {
        itemLang.add(new Label("label", itemLang.getModelObject()));
        Model<String> textModel = new Model<String>() {

@Override
            public String getObject() {
                return bean.map.get(itemLang.getModelObject()).toString;
            }

@Override
            public void setObject(String object) {
                bean.map.put(itemLang.getModelObject(), new Address(object));
            }
        };
        itemLang.add(new TextField<String>("email", textModel));
    }
};

and add it to your form.

Custom converters for addresses and strings will be an additional improvement.

Related Problems and Solutions