gwtbindinggxtgwt-editors

Binding a bean property to a TextField in GXT 3.0


I am working on a ExtGWT 3.0 (beta) application.

I have a simple Java bean containing one property:

public class MyBean {
    private String content;

    // getter and setter here...
}

I want to bind the property to a TextField.

I have created an interface:

interface MyBeanProperties extends PropertyAccess<MyBean> {
    ValueProvider<MyBean, String> content();
}

But what's next? How do I tell the TextField to bind to that particular property of a particular MyBean object?


Solution

  • PropertyAccess is used to generically refer to an objects properties, often for data widgets that use a Store like the grid or charts. For binding a form to a bean, check out GWT's editor framework at http://code.google.com/webtoolkit/doc/latest/DevGuideUiEditors.html. There are some examples for this with GXT at http://www.sencha.com/examples/#ExamplePlace:basicbinding%28uibinder%29

    Roughly, you'll build a form widget that wraps all the properties you need, and make an editor driver for that editor and its bean:

    public class MyBeanEditor implements Editor<MyBean> {
    
      // do any kind of widget setup you like, just make sure to have methods/fields
      // package protected or higher that extends Editor (Field extends Editor)
    
      TextField content;
    }
    
    //... declare the driver
    interface Driver extends SimpleBeanEditorDriver<MyBean, MyBeanEditor> {}
    
    //... use the driver to bind a form to a bean
    Driver driver = GWT.create(Driver.class);
    driver.initialize(myBeanEditorInstance);
    driver.edit(myBean);
    
    //... when save is clicked (or a timer, or whatever), get the value and do 
    //    something with it
    MyBean model = driver.flush();