Java Reference
In-Depth Information
Figure 19.12
Bean Builder invokes the set and get methods of the beans behind the
scenes.
If you have changed the properties of the Movie bean (and even if you
haven't), you will notice that the bean builder tool is invoking the appropriate set
and get methods on the bean object behind the scenes. If you typed in the Movie
bean class just as it appears earlier in this chapter, you will notice that I added
System.out.println() statements inside the set and get methods. The output of
these calls appears in the command prompt window that is running the Bean
Builder application. Figure 19.12 shows this window after some of the properties
of the Movie bean have been changed in the Property Inspector window.
Now that you have seen how to write a bean, package it in a JAR file, and
load it into the Bean Builder tool, I want to show you how the other types of
properties work in JavaBeans, which are bound properties and constrained
properties.
Bound Properties
A bound property of a JavaBean allows a bean property to be bound to the prop-
erty of another bean. When the property changes in the first bean, its changes
are automatically reflected in the second bean. This is a common aspect of Java-
Beans development, and the JavaBeans API contains the java.beans.Property-
ChangeSupport class to simplify the process of creating bound properties.
To give a bean bound properties, you perform the following steps:
1.
Add a field of type PropertyChangeSupport.
2.
Add the method addPropertyChangeListener() to the bean class, which
takes in a PropertyChangeListener object. Within this method, you add
the given listener object to the PropertyChangeSupport object.
3.
Add the method removePropertyChangeListener() to the bean class,
which also takes in a PropertyChangeListener object. Using the
PropetyChangeSupport object, remove the given listener.
4.
Add the method getPropertyChangeListeners(), which returns an array
containing all listeners currently bound to properties of this bean.
Search WWH ::




Custom Search