In this and the following sections, you'll make minor
modifications to the NervousText
bean to make it
more user friendly. Eventually you'll add a BeanInfo
class and a
PropertyEditor
class to control how the bean can be
customized inside a builder tool.
The BeanInfo
class
will allow the BeanBox to uncover information you specify about
the bean. Using a BeanInfo
class with your bean overrides the
information that the BeanBox's default introspection mechanism
discovers and presents to programmers working inside an
application builder tool.
In each of the examples that follow, you'll perform five steps:
- Step 1. Create the source file
- Step 2. Make the Bean automatically resizable
- Step 3. Compile the program
- Step 4. Build and install the jar
- Step 5. Test the Bean in the BeanBox
Step 1. Create the source file
You'll now be working on an example called NervousText05.java. First
copy NervousText04.java
to
NervousText05.java
and make the appropriate name
changes.. Change NervousText04
to
NervousText05
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
.
You may have noticed that if you type a very long label
name in the property editor for NervousText
, part of the
text gets truncated on the right side of the bean. You
can fix that by adding a method, sizeToFit
and calling the method from the setText
property setter, which is called every time a change is
made to the label.
Step 2. Make the bean automatically resizable
You'll now need to add the code for sizeToFit
.
Add the following code to the final example in NervousText04.java:
private void sizeToFit() {
Dimension d = getPreferredSize();
setSize(d.width, d.height);
Component p = getParent();
if (p != null) {
p.invalidate();
p.doLayout();
}
}
In addition, add the call to the sizeToFit
method in setText
.
Add the line:
sizeToFit();
Right after the call to getChaos
, the method should now look 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);
sizeToFit();
support.firePropertyChange(
"text", oldstring, newstring);
}
Now comment out, or delete, the old definition of preferredSize
in case you want to revert back to the old definition later.
/* Try dynamic sizing
// public Dimension preferredSize() {
// use this for a fixed size bean
public Dimension getMinimumSize() {
// use this for a variable size bean
return (new Dimension(150,150));
}
*/
This method is called by the BeanBox to decide how large the Bean
should be when displayed.
Rewrite the method as follows:
private void sizeToFit() {
Dimension d = getPreferredSize();
setSize(d.width, d.height);
Component p = getParent();
if (p != null) {
p.invalidate();
p.doLayout();
}
}
Step 3. Compile the program
Now you're ready to compile:
javac -d . NervousText05.java
Don't forget the `-d .' argument otherwise the class
file will be created in the wrong directory.
Step 4. Build and install the JAR
You can now make a JAR file from the Bean and place it
in the directory where the BeanBox expects to find JAR files.
You'll need a manifest file:
Manifest-Version: 1.0
Name: sun/beanbox/beans/NervousText05.class
Java-Bean: True
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.
JDK_HOME=/home_gvoss/work97/06/jdk1.1.2/jdk1.1.2
JDK_BIN=$(JDK_HOME)/bin
BDK_HOME=/home_gvoss/work97/06/bdk
JAR_DIR=$(BDK_HOME)/beans/jars
An example command sequence for installing the Bean in
the BeanBox by using the makefile
follows:
echo "Manifest-Version: 1.0" > manifest.tmp
echo "" >>
manifest.tmp
echo "Name: sun/beanbox/beans/NervousText05.class" >>
manifest.tmp
echo "Java-Bean: True" >>
manifest.tmp
jar cfm NervousText05.jar
manifest.tmp sun/beanbox/beans/NervousText05.class
cp -p NervousText05.jar BDK_HOME/beans/jars
Step 5. Test the Bean in the BeanBox
Run the BeanBox and drop a NervousText05 Bean on the form. See
what happens when you use the property editor to type in a string
larger than will fit in the original box surrounding the Bean.
Here is NervousText05.java
in its final form.