Spring : Binding Existing ComboBox to FormModel property

This page last changed on Apr 17, 2006 by Kees de Kooter

Intro

I create my forms using JFormDesigner (don't want to hack all those swing components manually). Spring RCP does not take gui builders into account (- yet, supposed to be supported in the 0.2 release). For "normal" fields this is no problem, you just do:

bindingFactory.bindControl(machineEditorPanel.getPricelistRemarksTextField(), 
            "priceListRemarks");

For comboboxes however there is no direct support. After taking a close look at how the code-only formbuilder works I came up with the following solution.

Solution

I extended the SwingBindingFactory.

public class ExtendedBindingFactory extends SwingBindingFactory {

    /**
     * @param arg0
     */
    public ExtendedBindingFactory(FormModel formModel) {

        super(formModel);
    }

    /**
     * @param comboBox
     * @param formPropertyPath
     * @param items
     * @param renderPropertyPath
     */
    public void bindComboBox(JComboBox comboBox, String formPropertyPath, 
            Collection items, String renderPropertyPath) {
        
        Map context = new HashMap(4);
        context.put(ComboBoxBinder.SELECTABLE_ITEMS_HOLDER_KEY, 
                new ValueHolder(items));
        context.put(ComboBoxBinder.RENDERER_KEY, 
                new BeanPropertyValueListRenderer(renderPropertyPath));
        context.put(ComboBoxBinder.EDITOR_KEY, 
                new BeanPropertyValueComboBoxEditor(renderPropertyPath));
        context.put(ComboBoxBinder.COMPARATOR_KEY, 
                new PropertyComparator(renderPropertyPath, true, false));
        
        this.bindControl(comboBox, formPropertyPath, context);
    }
}

And now binding comboxes is really easy (smile):

bindingFactory.bindComboBox(machineEditorPanel.getStatusComboBox(),
        "status", machineDao.findAllMachineStatuses(), "name");