The mechanism by which a bound property is connected with an interested party begins with two event listener registration methods,
addPropertyChangeListener() and removePropertyChangeListener().
These methods allow an interested party (listener) to bind itself to a property, thereby making the property bound.
Following are the definitions for these methods:
public void addPropertyChangeListener (PropertyChangeListener l);
public void removePropertyChangeListener (PropertyChangeListener l);
addPropertyChangeListener(PropertyChangeListener l) and removePropertyChangeListener(PropertyChangeListener l)
In the context of JavaBeans, the methods `addPropertyChangeListener(PropertyChangeListener l)` and `removePropertyChangeListener(PropertyChangeListener l)` play a critical role in enabling bound properties, which allow a bean to notify interested listeners when a property changes.
Hereâs how these methods work:
Bound Properties in JavaBeans:
A bound property is a property of a JavaBean that notifies other objects (listeners) when its value changes. This allows other components or objects to react to changes in the state of the bean. For example, when a UI component's value changes, other components or parts of the system can respond accordingly.
addPropertyChangeListener(PropertyChangeListener l):
This method registers a listener (an object that implements the `PropertyChangeListener` interface) with the JavaBean. The listener is then notified whenever a bound property changes.
The `PropertyChangeListener` interface defines a single method, `propertyChange(PropertyChangeEvent evt)`, which is called when a property change occurs.
The JavaBean invokes this listener when a bound property changes, passing a `PropertyChangeEvent` object that contains details about the change (such as the old and new values).
removePropertyChangeListener(PropertyChangeListener l): This method unregisters a listener, so that it will no longer receive notifications when a bound property changes. This is important for avoiding memory leaks and ensuring that the listener is not unnecessarily invoked once it no longer needs to be notified of changes.
Example Workflow of Bound Properties:
Property Change: A JavaBean has a property, say `age`, which is a bound property.
Listener Registration: An object that is interested in changes to the `age` property registers itself using `addPropertyChangeListener()`.
Property Update: When the `age` property is updated (e.g., a setter method changes the value), the bean fires a `PropertyChangeEvent` to all registered listeners.
Listener Notification: The registered listeners receive this event and can respond to the change, such as updating the UI or logging the change.
Listener Removal: If the listener no longer needs to be notified, it unregisters using `removePropertyChangeListener()`.
Code Example:
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class PersonBean {
private String name;
private int age;
private PropertyChangeSupport support;
public PersonBean() {
support = new PropertyChangeSupport(this);
}
public void setName(String name) {
String oldName = this.name;
this.name = name;
support.firePropertyChange("name", oldName, this.name);
}
public void setAge(int age) {
int oldAge = this.age;
this.age = age;
support.firePropertyChange("age", oldAge, this.age);
}
public void addPropertyChangeListener(PropertyChangeListener pcl) {
support.addPropertyChangeListener(pcl);
}
public void removePropertyChangeListener(PropertyChangeListener pcl) {
support.removePropertyChangeListener(pcl);
}
}
In this example:
The `PersonBean` class has two bound properties: `name` and `age`.
It uses `PropertyChangeSupport`, which helps manage listeners and notify them when a property changes.
The `addPropertyChangeListener()` and `removePropertyChangeListener()` methods allow other objects to listen for property changes.
In summary, these two methods (`addPropertyChangeListener()` and `removePropertyChangeListener()`) are fundamental for managing bound properties in JavaBeans, enabling a reactive and event-driven approach to property changes.
PropertyChangeListener interface
When an interested party wants to bind itself to a property, it must call addPropertyChangeListener()
and pass in an object implementing the PropertyChangeListener interface. The PropertyChangeListener interface supports the propertyChange() method, which is called whenever the bound property is changed. In other words, whenever the bound property changes, the propertyChange() method is called on the listener object, which is then passed into addPropertyChangeListener(). Incidentally, the removePropertyChangeListener() method allows an interested party to break a property binding.
Both of these registration methods are implemented in the PropertyChangeSupport class,
which is a utility class used in Beans that support bound properties.
In the next lesson, bound property registration methods will be discussed.