Java Beans
Java Beans
Java Beans
beans)
Interface Summary
Class Summary
BeanDescriptor A BeanDescriptor provides global information about a
"bean", including its Java class, its displayName, etc.
Exception Summary
IntrospectionException Thrown when an exception happens during Introspection.
A few of the classes are used by beans while they run in an application. For
example, the event classes are used by beans that fire property and vetoable
change events (eg. PropertyChangeEvent). However, most of the classes in this
package are meant to be used by a bean editor (that is, a development
environment for customizing and putting together beans to create an
application). In particular, these classes help the bean editor create a user
interface that the user can use to customize the bean. For example, a bean may
contain a property of a special type that a bean editor may not know how to
handle. By using the PropertyEditor interface, a bean developer can provide an
editor for this special type.
Long-Term Persistence
Writing out a bean, on the other hand, sometimes requires special knowledge of
the bean's type. If the bean's state can be expressed using only the no-argument
constructor and public getter and setter methods for properties, no special
knowledge is required.
If you need (or choose) to provide a persistence delegate for a bean, you can do
so either by using a DefaultPersistenceDelegate instance or by creating your own
subclass of PersistenceDelegate. If the only reason a bean needs a persistence
delegate is because you want to invoke the bean's constructor with property
values as arguments, you can create the bean's persistence delegate with the
one-argument DefaultPersistenceDelegate constructor. Otherwise, you need to
implement your own persistence delegate, for which you're likely to need the
following classes:
PersistenceDelegate
The abstract class from which all persistence delegates descend. Your
subclass should use its knowledge of the bean's type to provide
whatever Statements and Expressions are necessary to create the bean and
restore its state.
Interface Customizer
A customizer class provides a complete custom GUI for customizing a target Java
Bean.
Method Details
setObject
Set the object to be customized. This method should be called only once, before
the Customizer has been added to any parent AWT container.
Parameters:
addPropertyChangeListener
Register a listener for the PropertyChange event. The customizer should fire a
PropertyChange event whenever it changes the target bean in a way that might
require the displayed properties to be refreshed.
Parameters:
removePropertyChangeListener
A builder tool like NetBeans recognizes the method names and shows
the mouthWidth property in its list of properties. It also recognizes the type, int,
and provides an appropriate editor so the property can be manipulated at design
time.
This example shows a property than can be read and written. Other combinations
are also possible. A read-only property, for example, has a getter method but no
setter. A write-only property has a setter method only.
A special case for boolean properties allows the accessor method to be defined
using is instead of get. For example, the accessor for
a boolean property running could be as follows:
Various specializations of basic properties are available and described in the following sections.
Indexed Properties
An indexed property is an array instead of a single value. In this case, the bean
class provides a method for getting and setting the entire array. Here is an
example for an int[] property called testGrades:
Bound Properties
A bound property notifies listeners when its value changes. This has two
implications:
import java.beans.*;
public void
addPropertyChangeListener(PropertyChangeListener listener) {
mPcs.addPropertyChangeListener(listener);
}
public void
removePropertyChangeListener(PropertyChangeListener listener) {
mPcs.removePropertyChangeListener(listener);
}
}
Bound properties can be tied directly to other bean properties using a builder tool like NetBeans. You
could, for example, take the value property of a slider component and bind it to
the mouthWidth property shown in the example. NetBeans allows you to do this without writing any
code.
Constrained Properties
A constrained property is a special kind of bound property. For a constrained property, the bean keeps
track of a set of veto listeners. When a constrained property is about to change, the listeners are
consulted about the change. Any one of the listeners has a chance to veto the change, in which case
the property remains unchanged.
The veto listeners are separate from the property change listeners. Fortunately,
the java.beans package includes a VetoableChangeSupport class that greatly simplifies
constrained properties.
import java.beans.*;
public void
addPropertyChangeListener(PropertyChangeListener listener) {
mPcs.addPropertyChangeListener(listener);
}
public void
removePropertyChangeListener(PropertyChangeListener listener) {
mPcs.removePropertyChangeListener(listener);
}
public void
addVetoableChangeListener(VetoableChangeListener listener) {
mVcs.addVetoableChangeListener(listener);
}
public void
removeVetoableChangeListener(VetoableChangeListener listener) {
mVcs.removeVetoableChangeListener(listener);
}
}