Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

JavaBeans 101, Part III

 
Online Training Index

101, Part III:

Introduction | Page 1 | Page 2 | Page 3 | Page 4



Bound Properties

As you recall, JavaBeans use bound properties. This section adds 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 add bound property support to NervousText04. An object that supports bound properties can notify other objects when certain properties change. Such a notification is called a property change event. 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.

Step 1. Add an import statement to support property change events

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.*;

Step 2. Add code to track and notify interested property change listeners

Add the following code to maintain a list of listeners potentially wanting to be notified of bound 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 declares an instance variable, support, for NervousText04 objects. This variable holds a collection of listener objects that are notified about property changes to the bound property. The two supporting methods, addPropertyChangeListener and removePropertyChangeListener, provide a public interface that allows interested listeners to register themselves with NervousText04 objects.

Step 3. Add code to fire property change events

Modify the property setter method, setText, to fire a property change event whenever it is called. To do this, you 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 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;
    ...

The getText method returns the new value of the property.

When you have both the old and 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);
}

Step 4. Define event handler methods for property change events

Define a handler method, reportChange, that potentially can 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 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);

NervousText04 is now both a source for property change events and a handler for property change events. We haven't made it a listener because the BeanBox automatically generates a listener class as an adapter. The BeanBox creates an adapter when you connect an object that generates a property change to a target NervousText04 object 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.

Step 5. Build the JAR file and install it in the BeanBox

Compile NervousText04.java:

javac -d . NervousText04.java

Create the manifest file in a text editor:

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

Step 6. Test NervousText04 in the BeanBox

  • Start up the BeanBox.
  • Add one OurButton and two NervousText04 Beans.
  • Bind property changes in the first NervousText04 Bean to the second Bean.

The changes to the first NervousText04 are effected by pressing OurButton.

  • Change the label for OurButton to change direction. Also, make an action event in OurButton to cause the text to be reversed in the first NervousText04 Bean.
Program Source Code

A makefile for this lesson automates source code compilation, JAR file construction, and copying JAR files to the appropriate BeanBox directory. You have to edit several of the variables in the makefile to indicate the location of your JDK 1.1 and the BDK installation directories.

You may want to look at the final source file for NervousText04.java, to verify the changes you have made to the original NervousText.java file.

Coffecup Logo

Introduction | Page 1 | Page 2 | Page 3 | Page 4