Bound Properties

Beans use event delegation to manage properties. Components don’t exist in a vacuum; for instance, a change in the size property of a component might require other components to adjust their position. Or the setting of a certain state in a control might change another part of an application’s interface. A bound property notifies interested objects that it has undergone a change.

A bound property has a set function that fires a PropertyChangeSupport event; other components then declare themselves as listeners for that event. For example, I could modify the SimpleBean component to fire notification events when its background color changes.

public class SimpleBean extends Canvas 
{
    // New field to identify property change
    private PropertyChangeSupport changes =
        new PropertyChangeSupport(this);

    public Color getColor()
    {
        return getBackground();
    }

    public void setColor(Color new_color)
    {
        Color old_color = getBackground();
        setBackground(new_color);

        // Send change event to listeners
        changes.firePropertyChange("New color", old_color,
            new_color);
    }

    // Methods to manage listeners
    public void addPropertyChangeListener(
        PropertyChangeListener l)
    {
        changes.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(
        PropertyChangeListener l)
    {
        changes.removePropertyChangeListener(l);
    }
    
}

Notification granularity is per Bean, not per property; in other words, the Bean fires an event that identifies the property change. The implementation of bound properties is simply another use for the delegation event model.

© 1997 by Scott Ladd. All rights reserved.