javajavabeansbeans-binding

Sync object with values in GUI using Metawidget


I'm using Metawidget to automatically see/edit values in objects in the GUI. I'm able to bind the object's initial values, and see them in their respective GUI components. However, when I change the values in the GUI, these changes are not sync'ed back to the object. This is more or less documented here (deprecated) and here.

Here is my business object:

public static class Person {

    private String mName;

    public String getName() { return this.mName; }

    public void setName( String name ) { this.mName = name; }

    @UiAction
    public void showPersonObject() {
        JOptionPane.showMessageDialog(frame, this.mName);           
    }

    @UiAction
    public void bind() {
        metawidget.getWidgetProcessor( 
            BeansBindingProcessor.class)
                .save( metawidget );
    }
}

Here is my main method, where metawidget is configured:

public static void main( String[] args ) {
    // Person
    Person person = new Person();
    person.setName("A cool name");
    // Metawidget
    metawidget = new SwingMetawidget();
    metawidget.setInspector( new CompositeInspector(
        new CompositeInspectorConfig().setInspectors(
            new PropertyTypeInspector(),
            new MetawidgetAnnotationInspector(),
            new BeanValidationInspector())));
    metawidget.addWidgetProcessor( 
        new BeansBindingProcessor(
            new BeansBindingProcessorConfig().setUpdateStrategy(
                UpdateStrategy.READ_WRITE )) );
    metawidget.setToInspect( person );
    // Create Frame
    ...
}

In the documentation it is said that:

If set to READ or READ_WRITE (the default is READ_ONCE), the object being inspected must provide PropertyChangeSupport. If set to READ_WRITE, updates to the UI are automatically sync'ed back to the setToInspect, otherwise the client must manually call save:

myMetawidget.getWidgetProcessor( BeansBindingProcessor.class ).save( myMetawidget )

I've tried setting the UpdateStrategy to READ and/or READ_WRITE, and/or calling save() on BeansBindingProcessor. I've also tried to provide PropertyChangeSupport to the Person object (I think its refering to this), which is the same as adding the following code:

private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

public void addPropertyChangeListener(PropertyChangeListener listener) {
    this.pcs.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
    this.pcs.removePropertyChangeListener(listener);
}

public void setName( String name ) {
    String oldName = this.mName;
    this.mName = name;
    this.pcs.firePropertyChange("name", oldName, mName);
}

However, the Person object always maintains the original values.

Thanks in advance.


Solution

  • Well, I solved the problem. There is a "rogue" version of beansbinding.jar on the internet, that's why binding wasn't working. I used the version distributed with Metawidget examples, and now everything works fine.

    This problem is reported here.