Bean Internals  «Prev  Next»
Lesson 13More on bound property registration methods
ObjectiveLearn more about bound property registration methods.

Bound Property Registration Methods

In JavaBeans, "bound property registration methods" are part of the event-driven architecture that JavaBeans uses to handle changes in properties. A "bound property" is a property that notifies registered listeners when its value changes. This is crucial in creating interactive and responsive applications, particularly in user interface components where changes in the model need to reflect in the view.
Key Concepts of Bound Properties in JavaBeans
  1. Property Change Event: When the value of a bound property changes, a `PropertyChangeEvent` is fired. This event carries information about the old and new values of the property that changed.
  2. PropertyChangeListener: A class can implement the `PropertyChangeListener` interface to listen for changes in bound properties. This interface defines the `propertyChange()` method, which will be called when a bound property changes
  3. PropertyChangeSupport: Java provides the `PropertyChangeSupport` class to help manage bound property listeners. This class simplifies the process of adding and removing listeners, and of firing property change events. It provides methods like `addPropertyChangeListener()`, `removePropertyChangeListener()`, and `firePropertyChange()`.
  4. Bound Property Methods: JavaBeans use "bound property registration methods" to allow other objects to register themselves as listeners for a specific property change. These methods generally follow a naming pattern:
    • `addPropertyChangeListener(PropertyChangeListener listener)`: This method allows any object to register itself as a listener for property changes.
    • `removePropertyChangeListener(PropertyChangeListener listener)`: This method allows objects to unregister themselves from receiving property change events.
    • You can also have property-specific methods like `addPropertyChangeListener(String propertyName, PropertyChangeListener listener)` if you want to listen to changes in only a specific property.

Example of a Bound Property in JavaBeans

Here is an example of a JavaBean with a bound property:
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class Person {
    private String name;
    private PropertyChangeSupport support;

    public Person() {
        support = new PropertyChangeSupport(this);
    }

    public String getName() {
        return name;
    }

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

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        support.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        support.removePropertyChangeListener(listener);
    }
}

In this example:
  • The `Person` class has a bound property `name`.
  • The `PropertyChangeSupport` object `support` manages the listeners and handles the firing of property change events.
  • When `setName()` is called, the old and new values of `name` are passed to `firePropertyChange()`, which notifies all registered listeners.

Advantages of Using Bound Properties
  1. Separation of Concerns: The JavaBean itself doesn’t need to manage or know about all the listeners directly. The `PropertyChangeSupport` class handles that, making the code more modular.
  2. Decoupling: The object that changes the property does not need to know what happens with the change. Other objects (listeners) can react to these changes independently.
  3. Interactivity: Bound properties are a key component of event-driven systems, especially in GUI applications where changes in the model (e.g., user data) need to be immediately reflected in the view.

This mechanism enables easy notification of property changes and makes it easier to create applications with loosely coupled components that interact based on changes in state.


Two bound property Registration Methods

The two bound property registration methods you just learned about
public void addPropertyChangeListener  (PropertyChangeListener l);
public void removePropertyChangeListener  (PropertyChangeListener l);

apply to all properties, meaning that all bound properties will deliver notifications to a listener. It is up to the listener to determine which specific property changed.
Although this approach works fine, there is also a way for a listener to bind to a specific property. A Bean supports individual property binding by providing a pair of registration methods for each property.
For example, following is a pair of listener registration methods for a bound property named score:
public voidaddScoreListener(PropertyChangeListener l);
public voidremoveScoreListener(PropertyChangeListener l);

  • How these methods work:
    These methods work just like the global listener registration methods, except that they only apply to the score property. Per-property registration methods are always provided in addition to the global registration methods and primarily serve as a convenience. In the next lesson, you learn about constrained properties.

SEMrush Software