Binding with JGoodies Binding

The binding framework allows you to write classes that represents model completely independent of the user interface. The classes are bound to the user interface and are updated as the user interface enters information. The frame is based on the presentation pattern, described by Martin Fowler.

The framework already provides a Model class. Classes containing attributes that are editable on the user interface would be a subclass of this. It adds functionality to notify of events. By default, the frame assumes that the used naming conventions for the properties, accessors and mutator methods follow the same pattern as suggested in the Java beans specification. For the models that I create, I provide the property as a public constant within the model. The mutator code also need to call the model’s firePropertyChange method to notify listeners of a change to the property. For example, a model for a book could be the following:

public class Book extends Model 

     * References to the properties for the model.
    public static final String PROPERTY_TITLE = "title";

     * Keeps track of the title of the book. 
    private String title = " ";


     * Changes the title of the book.
     * @param newTitle
     *             The new title for the book.
    public void setTitle(String newTitle) 
        String oldTitle = title;
        title = newTitle;
        firePropertyChange(PROPERTY_TITLE, oldTitle, 
     * @return The title of the book.
    public String getTitle() 
        return title;

For creating the user interface components that will be editing the model, the PresentationModel is created for the object. The PresentationModel provides a representation of the user interface. The framework also supplies a BasicComponentFactory that you can use to create the components. The factory will also bind the component to the appropriate attribute. The methods that create text fields require a value model as one of its paramters. The value model is what is actually updated when the user makes changes in the component and it should be retrieved from the PresentationModel using its getModel method. For example, to create a text field for entering the title of a book:

        final Book subject = new Book();
        final PresentationModel<Book> presentation =
            new PresentationModel<Book>(subject);
        JTextField nameField = BasicComponentFactory.
                Book.PROPERTY_TITLE), false);

The third parameter, which is set to false will cause the bounded instance to be updated as the user makes changes in the text field. Setting it to true will result in the changes being made only when the field loses focus instead.

In the case of list selection fields, such as a JList and JComboBox, the required model is a SelectionInList. SelectionInList provides the functionality for tracking changes in the selection. Setting up a SelectionInList might require a few different calls.

    public static final String TOPICS[] = 
    {"science", "history", "arts"};
        SelectionInList<String> topicSelect =
            new SelectionInList<String>(TOPICS);

         * Set the presentation model as the model that
         * holds the selected value.
        JComboBox topicBox = BasicComponentFactory.

The SelectionInList class has a few different constructors that allow you to specify the set of possible options as a list or an array. The call to setSelectionHolder will make updates to the presention model as the selection of the value is changed.

The BasicComponentFactory can also create JLabel that displays the current value of an attribute in the model. The label gets updated as the value of the attribute changes.

        JLabel titleLabel = BasicComponentFactory.
        JLabel topicLabel = BasicComponentFactory.

Since the title property has been bound to the title text field, the titleLabel will be automatically updated when the user makes changes in the field. Similarly, topic property has been bound such that when the user changes its selection, it the topicLabel will be updated. If you wish to try out the examples from here, you can get the source code here.


2 Responses to Binding with JGoodies Binding

  1. Pingback: Controlling UI Components using JGoodies Binding « Kah – The Developer

  2. santosh says:

    simple and smart thanks alot

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: