If you're building beans, you want to know about bound properties. An object that supports bound properties can notify other objects when certain property values change. Such a notification is called a property change event. This section starts by adding support for bound properties to the evolving NervousText example. The example code shows how to fire property events, how components maintain listeners wishing to be notified of change events, and how to respond to property change event notices.
In this section you will be adding bound property support to NervousText04. An object that supports bound properties can notify other objects when certain properties change. Each time a bound property changes, it notifies registered listener objects by calling specified property change handler methods defined for the listener. These methods are called with the new and old values of the property, as well as name of the property that has changed.
For a bean to generate or receive property change events, you must add the following import statement to the top of the file.
import java.beans.*;
The package and import statements at the top of the file should
be as follows:
package sun.beanbox.beans;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.*;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.beans.*;
Add the following code to maintain a list of listeners potentially wanting to be notified of text property changes:
public void addPropertyChangeListener(PropertyChangeListener l) {
support.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l) {
support.removePropertyChangeListener(l);
}
private PropertyChangeSupport support = new PropertyChangeSupport(this);
The last statement in this listing declares an instance variable,
support, for NervousText04 objects. This variable
holds a collection of listener objects that will be notified about
property changes to the text property. The two supporting methods,
addPropertyChangeListener and
removePropertyChangeListener provide a public interface,
allowing interested listeners to register themselves with
NervousText04 objects.
text Property Change Events
Modify the text property setter method to fire a property
change event whenever it is called. To do this, you need to save
copies of both the old and new values of the property being changed.
To save the old property, add the following line to the end of the
setText method in NervousText04:
String oldstring = s;
Recall that "s" is the value of the text property accessed by the
getText and setText getter and setter
methods for the text property. The instance variable s
is declared at the top of the class:
public class NervousText04 extends Panel implements Runnable, MouseListener {
...
String s = null;
...
Now that you have both old a new values saved in local variables, add
the following line to the end of the setText method:
support.firePropertyChange("text", oldstring, newstring);
The full method now looks like this:
public void setText(String newstring){
String oldstring = s;
s=new String(newstring);
separated= new char[s.length()];
s.getChars(0,s.length(),separated,0);
support.firePropertyChange("text", oldstring, newstring);
}
Define a handler method that can potentially be called by a listener object to report that a text property has changed.
public void reportChange(PropertyChangeEvent evt) {
System.err.println("ENTER---> NervousText04 reportChange");
String oldValue = evt.getPropertyName() + " := " + evt.getOldValue();
String newValue = evt.getPropertyName() + " := " + evt.getNewValue();
System.err.println("New value: " + oldValue);
System.err.println("Old value: " + newValue);
System.err.println("EXIT----> NervousText04 reportChange");
}
Add the above code for reportChange at the end of the
class definition for NervousText04 immediately after the declaration
for the support variable you added earlier:
private PropertyChangeSupport support = new PropertyChangeSupport(this);
Now NervousText04 is both a source for property change events and
a handler for property change events. We haven't made it a listener.
That's because the BeanBox will automatically generate a listener
class as an adapter. An adapter is created when you connect any
object that generates a property change to a NervousText04 object as a
target when building a BeanBox application.
The reportChange method does not actually cause a
change to take place in the target object. To do that, define an
additional method called makeChange as follows:
public void makeChange(PropertyChangeEvent evt) {
System.err.println("ENTER---> NervousText04 makeChange");
String oldValue = (String)evt.getOldValue(); // (String) cast required since
String newValue = (String)evt.getNewValue(); // evt.getOldValue() returns an Object
setText("*" + newValue + "*");
System.err.println("New value: " + oldValue);
System.err.println("Old value: " + newValue);
System.err.println("EXIT----> NervousText04 makeChange");
}
Add the method definition for makeChange
immediately after the code for reportChange. The
actual change in the target NervousText object occurs when the setter
method, setText , is called in the makeChange
event handler method.
Compile NervousText04.java:
javac -d . NervousText04.java
Create the mainfest:
echo "Manifest-Version: 1.0" > manifest.tmp
echo "" >> manifest.tmp
echo "Name: sun/beanbox/beans/NervousText04.class" >> manifest.tmp
echo "Java-Bean: True" >> manifest.tmp
The resulting temporary manifest file should look
like this:
Manifest-Version: 1.0
Name: sun/beanbox/beans/NervousText04.class
Java-Bean: True
Create the JAR file:
jar cfm NervousText04.jar manifest.tmp sun/beanbox/beans/NervousText04.class
Remove the temporary manifest and install the JAR in the BeanBox jars directory (substituting your BDK installation directory for BDK_HOME:
rm manifest.tmp
cp -p NervousText04.jar BDK_HOME/beans/jars
Start up the BeanBox. Add one OurButton, and two NervousText04 Beans. You're going to bind cha