In this section you will learn more about Beans and the
BeanBox by
- Creating a simple Bean
- Compiling and saving the Bean into a
Java Archive (JAR) file
- Loading the Bean into the ToolBox
- Dropping a Bean instance into the BeanBox
- Inspecting the Bean's properties, methods, and events
- Generating an introspection report
Your Bean will be namedSimpleBean.
Here are the steps to create it and view it in the BeanBox:
- Write the
SimpleBeancode. Put it in a file
namedSimpleBean.java, in the directory
of your choice. Here's the code:
import java.awt.*;
import java.io.Serializable;
public class SimpleBean extends Canvas
implements Serializable
{
//Constructor sets inherited properties
public SimpleBean(){
setSize(60,40);
setBackground(Color.red);
}
}
SimpleBeanextends thejava.awt.Canvascomponent.SimpleBeanalso implements thejava.io.Serializableinterface,
a requirement for all Beans.SimpleBeansets the background
color and component size.
- Make sure the
CLASSPATH
environment variable is set to point to all
needed.class(or.jar) files. Here
are some URLs that will help you to set CLASSPATH correctly:
- The
Managing Source and Class Files lesson gives good advice on how and when to set your CLASSPATH.
- The JDK Tool Reference Page provides complete CLASSPATH information for both
Windows and
Solaris platforms.
- Compile the Bean:
javac SimpleBean.java
This produces the class fileSimpleBean.class
- Create a manifest file. Use your favorite text editor
to create a file, we'll call itmanifest.tmp,
that contains the following text:
Name: SimpleBean.class
Java-Bean: True
- Create the JAR file. The JAR file will contain the
manifest and theSimpleBeanclass file:
jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
See the
Packaging Programs in JAR Files trail, and the
JDK JAR file documentation for complete information on JAR files.
- Load the JAR file into the ToolBox. Select
the File|LoadJar... menu item. This will bring up a
file browser. Navigate to theSimpleBean.jarlocation and
select it.SimpleBeanwill appear at the bottom
of the ToolBox. (Note that when the BeanBox is
started, all Beans in JAR files in the
beans/jarsdirectory are automatically
loaded into the ToolBox).
- Drop a
SimpleBeaninstance into the BeanBox.
Click on the wordSimpleBeanin the ToolBox. The cursor
will change to a crosshair. Move the cursor to a spot within the
BeanBox and click. SimpleBean will appear as a
painted rectangle with a hatched border. This border means
thatSimpleBeanis selected. The
SimpleBeanproperties will appear in the
Properties sheet.
You can resizeSimpleBean, because it inherits fromCanvas, by dragging a corner. You will see the cursor
change to a right angle when over a corner. You can also repositionSimpleBeanwithin the BeanBox by dragging on any non-corner
portion of the hatched border. You will see the cursor change to crossed
arrows when in position to move the Bean.
SimpleBean Makefiles
Below are two makefiles (Unix and Windows) set up to
createSimpleBean.
# gnumake file
CLASSFILES= SimpleBean.class
JARFILE= SimpleBean.jar
all: $(JARFILE)
# Create a JAR file with a suitable manifest.
$(JARFILE): $(CLASSFILES) $(DATAFILES)
echo "Name: SimpleBean.class" >> manifest.tmp
echo "Java-Bean: True" >> manifest.tmp
jar cfm $(JARFILE) manifest.tmp *.class
@/bin/rm manifest.tmp
# Compile the sources
%.class: %.java
export CLASSPATH; CLASSPATH=. ; javac $<
# make clean
clean:
/bin/rm -f *.class
/bin/rm -f $(JARFILE)
Here is the Windowsnmakeversion:
# nmake file
CLASSFILES= simplebean.class
JARFILE= simplebean.jar
all: $(JARFILE)
# Create a JAR file with a suitable manifest.
$(JARFILE): $(CLASSFILES) $(DATAFILES)
jar cfm $(JARFILE) <<manifest.tmp *.class
Name: SimpleBean.class
Java-Bean: True
<<
.SUFFIXES: .java .class
{sunw\demo\simple}.java{sunw\demo\simple}.class :
set CLASSPATH=.
javac $<
clean:
-del sunw\demo\simple\*.class
-del $(JARFILE)
You can use these makefiles as templates for creating your own
Bean makefiles. The example Bean makefiles, in thebeans/demodirectory, also show you how to use
makefiles to build and maintain your Beans.
 
Inspecting SimpleBean Properties and Events
The Properties sheet displays the selected Bean's properties.
WithSimpleBeanselected, the Properties
sheet displays four propeties:foreground,background,font, andname.
We declared no properties inSimpleBean
(see the Properties section to learn
how to declare properties), so these are properties inherited fromCanvas. Clicking on each property brings up a property
editor. The BeanBox provides default property editors
for the primitive types, plusFontandColor
types. You can find the sources for these property editors inbeans/apis/sun/beans/editors.
Beans communicate with other Beans by sending and
receiving event notifications.
To see which eventsSimpleBeancan send, choose
the Edit|Events BeanBox menu item. A list of events,
grouped by the Java interface in which the event method is
declared, will be displayed. Under each interface
group is a list of event methods. These are all
inherited from Canvas.
You will learn more about
properties and
events in
upcoming sections.
Generating Bean Introspection Reports
Introspection is the process of discovering a
Bean's design-time features by one of two
methods:
- Low-level reflection, which uses design
patterns to discover your Bean's features
- By examining an associated
bean information
class that explicitly describes your Bean's features.
You can generate a Bean introspection report
by choosing the Edit|Report menu item. The
report lists Bean events, properties, and methods,
and their characteristics.
By default Bean reports are sent
to thejavainterpreter's standard output, which is the window
where you started the BeanBox. You can redirect the
report to a file by changing the java interpreter
command inbeanbox/run.shorrun.batto:
java sun.beanbox.BeanBoxFrame > beanreport.txt
 


No comments:
Post a Comment