Skip to main content

Gooey Beans Form

Posted by evanx on December 29, 2006 at 10:03 AM EST
Gooey Beans Form

We use an MVC architecture, with some simple "beans binding" from our form to our model. Our "model" is a POJO, as in the Presentation Model pattern. We explicitly declare our properties in a model info class as presented earlier in Gooey Beans Info.

In our controller we create "adapters" for the input components in our form, eg. JTextField, JFormattedTextField, JCheckBox and JComboBox. Our form might be created using Matisse, so we bind our adapters to our form's (private) input components using reflection.


Code Snippet

We implemented a model object in Gooey Beans Info, and a form in Group Layout Therapy. We now implement the controller as follows.

public class PersonInfoController implements ActionListener, QGooeyWorksheet {    
    ...
    PersonInfoForm form = new PersonInfoForm();
    PersonInfoModel model = new PersonInfoModel();
    PersonInfoModelInfo modelInfo = new PersonInfoModelInfo();
    
    QGooeyFormHelper<PersonInfoModel> gooey = 
            new QGooeyFormHelper(this, model);
    ...    
    QTextFieldAdapter lastName =
            gooey.createTextFieldAdapter(modelInfo.lastName);    
    QFormattedTextFieldAdapter dependents =
            gooey.createFormattedTextFieldAdapter(modelInfo.dependents);
    QCheckBoxAdapter confirmed =
            gooey.createCheckBoxAdapter(modelInfo.confirmed);
    QComboBoxAdapter title =
            gooey.createComboBoxAdapter(modelInfo.title);
    ...    
    public PersonInfoController() {
        gooey.setForm(form);
    }
    
    public void actionPerformed(ActionEvent event) {
        try {
            gooey.refreshModel();
            ... // process model
            gooey.refreshForm();
            gooey.publishInfo(Level.INFO, null);
        } catch (Exception e) {
            gooey.publishException(e, Level.WARNING, null);
        }
    }
    ...
}

where the component adapters are created with a bean property courtesy of our "model info" class. They enable beans binding to our POJO presentation model object. We provide methods to refresh our model from the form, and visa versa, as seen in the above actionPerformed() method.

Our input component adapters are connected to the components on our form using reflection, when we invoke setForm() in the above constructor. If this form is created using Netbeans, then those components are private. That's fine because we manipulate them in our controller via the adapters.


Demo

We continue with the same demo as in the prequel Gooey Beans Info.

Launch   (PersonInfo, 150k/500k, unsandboxed, Java6)


Related Topics >>