In this section you'll make minimal changes to the actual
NervousText
Bean. Actually, the only changes made are name
changes for the new version of the NervousText06.java
source file. Instead you'll concentrate on adding a BeanInfo
class
associated with NervousText
. The class name will be
NervousText06BeanInfo
.
In this example, you'll perform the following steps:
- Step 1. Create the source file
- Step 2. Create the
NervousText06BeanInfo
class (subclass SimpleBeanInfo
)
- Step 3. Define a
getBeanDescriptor
method
- Step 4. Define a
getPropertyDescriptors
method
- Step 5. Compile the programs
- Step 6. Build and install the jar
- Step 7. Test the Bean in the BeanBox
Step 1. Create the source file
You'll now be working on an example called NervousText06.java. First
copy NervousText05.java
to
NervousText06.java
and make the appropriate name
changes. Change NervousText05
to
NervousText06
throughout the file. You'll
want to do a global search and replace as some of the
diagnostic strings print the class name to stdout
.
Those are the only changes required. Now for the BeanInfo class.
Step 2. Create the NervousText06BeanInfo
class
Let's say your marketing department wants to put the right spin on
the name for NervousText when viewed by the public from inside builder
tools. The name of this Bean might make programmers a little uneasy.
Instead of the class name, NervousText06
appearing as the
Bean name inside of builder tools, the marketing folks would like the
string `Uneasy Text'
to be displayed.
This kind of name change is typical of the way Beans evolve.
Initially you'll put in a lot of time worrying about how to build the
Bean, but by the time you go to sell it, it may not be convenient to
change class names. Also, arbitary strings describing the Bean give
you more flexibility than the syntax allowed for Java class names.
Create the NervousText06BeanInfo
class in a file
called NervousText06BeanInfo.java
.
Here it is in skeletal form:
package sun.beanbox.beans;
import java.beans.*;
public class NervousText06BeanInfo extends
SimpleBeanInfo {
...
}
First of all, you're
extending the
SimpleBeanInfo
class. This is generally what you'll want to do when defining
a BeanInfo
. By convention you should name the BeanInfo
class
to correspond to your Bean class by appending the text `BeanInfo
' to
your class name.
SimpleBeanInfo
implements the
BeanInfo
interface.
Fill in the class code by defining a method that will return
a
BeanDescriptor
to the builder tool when it looks to see if the Bean has one. The builder will
call a method named getBeanDescriptor
, which must override if you
don't want the default implementation defined in SimpleBeanInfo
.
Step 3. Define a getBeanDescriptor
method
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd;
...
return bd;
}
Now for the details.
You'll also need to call a method defined in BeanDescriptor
,
surprisingly called getBeanDescriptor
. Create an instance
of a BeanDescriptor
associated with your Bean, in this case
the NervousText06
class.
BeanDescriptor bd = new BeanDescriptor(beanClass);
The argument to the constructor is the Class
object for your Bean. For example,
JDK 1.1 introspection allows you
to get the Class object associated
with a class, MyClass using the
construct
MyClass.class
In this example, the Bean class
is declared as a static constant inside
the
NervousText06BeanInfo
class:
private final static Class beanClass =
NervousText06.class;
Once you have the Bean descriptor, you can set
the name used by builder tools by calling
setDisplayName
with the
desired name as a String
argument.
bd.setDisplayName("Uneasy Text");
The BeanDescriptor
object must be returned by
getBeanDescriptor
method in your BeanInfo
class so it can be created and used from within the builder tool.
You may also find it handy to include some println
for debugging. This is a good way to be sure the method actually gets
called by the builder tool if you run into trouble later.
public BeanDescriptor getBeanDescriptor() {
System.err.println("ENTER--->
NervousText06BeanInfo.getBeanDescriptor");
...
System.err.println("EXIT---->
NervousText06BeanInfo.getBeanDescriptor");
...
}
If you're following along,
your method should now look like this:
public BeanDescriptor getBeanDescriptor() {
System.err.println("ENTER--->
NervousText06BeanInfo.getBeanDescriptor");
BeanDescriptor bd = new BeanDescriptor(beanClass);
bd.setDisplayName("Uneasy Text");
System.err.println("EXIT---->
NervousText06BeanInfo.getBeanDescriptor");
return bd;
}
Step 4. Define a getPropertyDescriptors
method
In addition to providing a
BeanDescriptor
for your Bean, you might want to limit
the number of properties that are
displayed in the property sheet.
This is very likely if your Bean
is a subclass of one of the AWT
components, which can define many
more properties than you want
a programmer to see. You don't
want to overwhelm someone with
a larger number of choices unless
you're also going to provide a
custom property editor or wizard.
You'll see how to build a simple
custom property editor in the
next section.
Start with the method skeleton including
the exception handling code.
public PropertyDescriptor[] getPropertyDescriptors() {
try {
...
} catch (IntrospectionException e) {
throw new Error(e.toString());
}
}
Next, declare an instance
of a
PropertyDescriptor
inside the try
block.
PropertyDescriptor textPD =
new PropertyDescriptor("text", beanClass);
...
You're going to be returning an
array of property descriptors. The array
will contain all the properties
you want to be visible to the builder
tool. In this case, there will only
be one for the text
property controlling
the label for NervousText
.
Create the array of property descriptors
and add the text property descriptor to it:
PropertyDescriptor rv[] = {textPD};
...
An initializer is used declaring a constant
array of one element, textPD
.
The array can now be returned to the
builder tool.
...
return rv;
You should now have the following method
definition inside NervousText06BeanInfo
.
public PropertyDescriptor[] getPropertyDescriptors() {
try {
PropertyDescriptor textPD =
new PropertyDescriptor("text", beanClass);
PropertyDescriptor rv[] = {textPD};
return rv;
} catch (IntrospectionException e) {
throw new Error(e.toString());
}
}
Here's what the final source should look like for
NervousText06BeanInfo.java
package sun.beanbox.beans;
import java.beans.*;
public class NervousText06BeanInfo
extends SimpleBeanInfo {
private final static Class beanClass =
NervousText06.class;
public BeanDescriptor getBeanDescriptor() {
System.err.println("ENTER--->
NervousText06BeanInfo.getBeanDescriptor");
BeanDescriptor bd = new BeanDescriptor(beanClass);
bd.setDisplayName("Uneasy Text");
System.err.println("EXIT---->
NervousText06BeanInfo.getBeanDescriptor");
return bd;
}
public PropertyDescriptor[] getPropertyDescriptors() {
try {
PropertyDescriptor textPD =
new PropertyDescriptor("text", beanClass);
PropertyDescriptor rv[] = {textPD};
return rv;
} catch (IntrospectionException e) {
throw new Error(e.toString());
}
}
}
Step 5. Compile the programs
javac -d . NervousText06.java
javac -d . NervousText06BeanInfo.java
Step 6. Build and install the JAR
Now you have two classes to add to your JAR file;
one is a Bean, the other isn't. However, you only
need one manifest and one JAR.
The manifest should look like this:
Manifest-Version: 1.0
Name: sun/beanbox/beans/NervousText06.class
Java-Bean: True
Name: sun/beanbox/beans/NervousText06BeanInfo.class
A makefile will make
the manifest for you if you don't want to do it by hand. It will also
build the JAR file and copy it to the right location provided that you
have set the target directories at the top of the file.
echo "Manifest-Version: 1.0" > manifest.tmp
echo "" >>
manifest.tmp
echo "Name: sun/beanbox/beans/NervousText06.class"
>>
manifest.tmp
echo "Java-Bean: True" >>
manifest.tmp
echo "" >>
manifest.tmp
echo "Name: " +
"sun/beanbox/beans/NervousText06BeanInfo.class"
>>
manifest.tmp
jar cfm NervousText06.jar manifest.tmp \
sun/beanbox/beans/NervousText06.class \
sun/beanbox/beans/NervousText06BeanInfo.class
cp -p NervousText06.jar BDK_HOME/beans/jars
Be sure to substitute your BDK installation directory
for BDK_HOME when you copy the JAR.
Step 7. Test the bean in the BeanBox
Run the BeanBox and drop a NervousText06 bean on the form.
Notice that the name for the bean is
now `Uneasy Text
'.
The marketing department is happy.
Also, have a look at the property and see how it differs
from the previous example. There should now
only be one field that shows up, the text
property for NervousText06
.
For your convenience, here is a consolidated list of the JDK
classes used in this example:
BeanInfo
SimpleBeanInfo
BeanDescriptor
PropertyDescriptor
Here is NervousText06.java and
NervousText06BeanInfo.java
in their final forms.