Java Reference
In-Depth Information
beans must be of the same type as that of the bound property. The relevant classes
to achieve this linkage are contained within package java.beans . The objects to be
notifi ed are registered as PropertyChangeListener s. A PropertyChangeSupport
object maintains a list of these listeners. The constructor for this object takes one
argument: the source bean. For example:
PropertyChangeSupport changeSupport =
new PropertyChangeSupport(this);
In this example, the PropertyChangeSupport object has been created within the
source bean itself.
The PropertyChangeSupport object notifi es any registered listeners of a change
in the bound property via method fi rePropertyChange , which takes three
a String , identifying the bound property;
an Object , identifying the old value;
an Object , identifying the new value.
Since the second and third arguments must be of type Object or a subclass of this
(i.e., an object of any class), any primitive value must be converted into an object by
the appropriate 'wrapper' class ( Integer , Float , etc.). For example: rePropertyChange(
"boundProp",new Integer(oldVal),new Integer(newVal));
Execution of the above method causes PropertyChangeEvent objects to be
generated automatically (and transparently).
The changes in the source bean required to achieve all this are summarised in the
steps below.
1. Add the line : import java.beans.*;
2. Create a PropertyChangeSupport object, using this as the single argument to the
3. Defi ne methods addPropertyChangeListener and removePropertyChange-
Listener , specifying a PropertyChangeListener argument and void return type.
(Defi nitions of the above methods simply call up the corresponding methods of
the PropertyChangeSupport object, passing a listener argument.)
4. Extend the 'set' method for the bound property to call method fi rePropertyChange .
This is a further extension of our animation bean, using imageName as the bound
property. The code changes are shown in bold text below.
package animBeans;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Search WWH ::

Custom Search