Java Reference
In-Depth Information
that sends out a notification event when its value changes and allows the change
to be vetoed by listeners.
Because Java allows dynamic loading of classes, beanbox programs can load arbi-
trary beans. The beanbox tool determines the properties, events, and methods a
bean supports with an introspection mechanism that is based on the
java.lang.reflect reflection mechanism for obtaining information about the
members of a class. A bean can also provide an auxiliary BeanInfo class that sup-
plies additional information about the bean. The BeanInfo class provides this addi-
tional information in the form of a number of FeatureDescriptor objects, each of
which describes a single feature of the bean. FeatureDescriptor has a number of
subclasses: BeanDescriptor , PropertyDescriptor , IndexedPropertyDescriptor ,
EventSetDescriptor , MethodDescriptor , and ParameterDescriptor .
A primary task of a beanbox application is to allow the user to customize a bean
by setting property values. A beanbox defines property editors for commonly used
property types, such as numbers, strings, fonts, and colors. If a bean has a prop-
erty of a more complicated type, however, it may need to define a PropertyEdi-
tor class that enables the beanbox to let the user set values for that property.
In addition, a complex bean may not be satisfied with the property-by-property
customization mechanism provided by most beanboxes. Such a bean may want to
define a Customizer class, which creates a graphical interface that allows the user
to configure a bean in some useful way. A particularly complex bean may even
define customizers that serve as “wizards” that guide the user step-by-step through
the customization process.
A Simple Bean
As noted earlier, Swing and AWT components can all function as beans. When you
write a custom GUI component, it is not difficult to make it function as a bean as
well. Example 14-1 shows the definition of a custom JavaBeans component, Mul-
tiLineLabel , that displays one or more lines of static text. This is something the
AW T Label component can't do.
What makes this component a bean is that all its properties have get and set
accessor methods. Because MultiLineLabel doesn't respond to user input in any
way, it doesn't define any events, so no event listener registration methods are
required. MultiLineLabel also defines a no-argument constructor, so that it can be
easily instantiated by beanboxes.
Example 14−1: MultiLineLabel.java
package com.davidflanagan.examples.beans;
import java.awt.*;
import java.util.*;
/**
* A custom component that displays multiple lines of text with specified
* margins and alignment. In Java 1.1 we could also subclass Component,
* making this a "lightweight" component. Instead, we try to maintain
* Java 1.0 compatibility for this component. This means that you will see
* deprecation warnings when you compile this class with Java 1.1 or later.
Search WWH ::




Custom Search