Overview of JavaBeans
Project
This page is edited by Kali Wong
&
Modified by Hsiang-Ling Hu
The material of this page is based
on the following references.
The information shown below is
edited from the following references.
Please refer to the original sites
listed below.
Thanks to the authors of
the following references.
1. JavaBean
concepts
2. BeanBox
Tutorial
3. Starting
and Using the BeanBox
4. Writing
a SimpleBean
5. DOWNLOADING
the BDK 1.0
*** What is a Java Bean ***
Java Beans are Javasoft's approach to component oriented software development,
that is making it possible to develop software components that relatively
easily can be assembled into an application by an enduser or a programmer.
The ability to be manipulated by a application builder tool (ABT) is the
defining characteristic of a Java Bean.
A notable difference between Java Beans and other component architectures
(such as COM or OpenDoc) is that Java Beans are Java-centric, exclusively
targeted at software development in Java. To ease integration with the
Windows platform, bridges between Beans and COM have been developed.
JavaBeans brings component technology to the Java platform. You can
use JavaBeans to write Java classes, called Beans, that you can visually
manipulate within application builder tools. This document is a hands-on
guide to learning JavaBeans and the Beans Development Kit (BDK). The JavaBeans
API Specification provides a complete JavaBeans description.
The software you'll need to understand and explore Beans is available
free on the web. In addition to the Beans
Development Kit (BDK), you will need the Java
Development Kit (JDK).
*** JavaBeans Concepts ***
The following information is from JavaBean
concepts (reference 1).
The JavaBeans API makes it possible to write component software in the
Java programming language. Components are self-contained, reusable software
units that can be visually composed into composite components, applets,
applications, and servlets using visual application builder tools. JavaBean
components are known as Beans.
Components expose their features (for example, public methods and events)
to builder tools for visual manipulation. A Bean's features are exposed
because feature names adhere to specific design patterns. A "JavaBeans-enabled"
builder tool can then examine the Bean's patterns, discern its features,
and expose those features for visual manipulation. A builder tool maintains
Beans in a palette or toolbox. You can select a Bean from the toolbox,
drop it into a form, modify it's appearance and behavior, define its interaction
with other Beans, and compose it and other Beans into an applet, application,
or new Bean. All this can be done without writing a line of code.
*** BDK ***
The following information is from JavaSoft
Home site.
The JavaBeans Development Kit (BDK) is intended to support the early
development of JavaBeans components and to act as a standard reference
base for both component developers and tool vendors. The BDK provides a
reference Bean container, the "BeanBox" and a variety of reusable example
source code (in the demo and beanbox subdirectories) for use by both JavaBeans
component and tool developers.
The BDK is not intended for use by application developers, nor is it
intended to be a full-fledged application development environment. Instead,
application developers should consider the various Java application development
environments supporting JavaBeans.
This BDK 1.0 release requires you to have installed the Java Development
Kit (JDK) 1.1 or later. We recommend using JDK
1.1.4 or later.
The BDK is qualified for Solaris 2.4, Solaris 2.5, Windows 95, and Windows
NT 4.0. However the BDK is "pure Java" and should run on any JDK 1.1 enabled
system.
*** Starting and Using the BeanBox ***
The following information is from Starting
and Using the BeanBox ( reference 3)
and BeanBox
Tutorial (reference 2).
You can use these commands to start the BeanBox, The beans/beanbox.
directory contains Windows (run.bat) and Unix (run.sh) or use make:
gnumake run
Or:
nmake run
See the BDK files beans/doc/makefiles.html and beans/doc/gnu.txt
for information about getting copies of these make tools.
When started, the BeanBox displays three windows: The BeanBox, ToolBox,
and the Properties sheet. Here's how they look:
The picture shown above is
from
Starting
and Using the BeanBox ( reference 3).
This illustration shows the BeanBox with the Juggler demo Bean placed
within it. The hatching around the Juggler is how the BeanBox indicates
the selected Bean. Clicking on a Bean selects it.
Adding a Bean to the ToolBox
The ToolBox contains the Beans available for use by the BeanBox. When the
BeanBox is started, it automatically loads all the Beans it finds within
the JAR files contained in the beans/jars directory.
Move your JAR files into that directory to have them automatically loaded
at BeanBox startup. Load Beans from JAR files located elsewhere by using
the File|LoadJar... BeanBox menu item. Clicking on a Bean name within the
ToolBox chooses that Bean for placement within the BeanBox. You will see
the cursor change to a crosshair.
Dropping a Bean on the BeanBox
To add a JellyBean to the BeanBox
-
Click on the word JellyBean in the ToolBox. The cursor
will change to a crosshair.
-
Click within the BeanBox. The JellyBean will appear and
will be selected.
Note the change in the Properties sheet when you put the JellyBean
in the BeanBox. Before you placed the JellyBean
in the BeanBox, the BeanBox's properties were displayed; after placing
the JellyBean in the BeanBox, the
JellyBean
properties are displayed. If you missed the change, click within the BeanBox,
away from the JellyBean. This will
select the BeanBox rather than the JellyBean.
The Properties sheet will then display the BeanBox's properties.
The Properties sheet displays the selected Bean's properties. After
dropping a JellyBean instance on the BeanBox, the Properties sheet
displays the JellyBean properties: color, foreground, priceInCents,
background, and font.
Editing Bean Properties
The Properties sheet displays each property's name and its current value.
Values are displayed in an editable text field (strings and numbers), a
choice menu (booleans), or as painted values (colors and fonts). You can
edit the property value by clicking on a property within the Properties
sheet. Properties displayed in text fields or choice menus are edited within
the Properties sheet. When you click on a color or font property a separate
panel, called a property editor, will pop up to do the editing.
These property types use a custom property editor. Try clicking
on each of the JellyBean properties.
Saving and Restoring Beans
The BeanBox uses Java Object Serialization to save and restore Beans and
their state. The following steps demonstrate how to save and restore a
Bean:
-
Drop a JellyBean on the BeanBox.
-
Change the color property to anything you want.
-
Select the File|Save BeanBox menu item. A file browser will pop up; use
it to save the Bean to a file.
-
Select the File|Clear BeanBox menu item.
-
Select the File|Load BeanBox menu item. The file browser will again pop
up; use it to retrieve the serialized Bean.
*** Writing a SimpleBean ***
The following information is from Writing
a SimpleBean (reference 4).
In this section you will learn more about Beans and the BeanBox by
-
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 named SimpleBean. Here are the steps to create
it and view it in the BeanBox:
-
Write the SimpleBean code. Put it in a file named SimpleBean.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);
}
}
SimpleBean extends the java.awt.Canvas
component. SimpleBean also implements the
java.io.Serializable
interface, a requirement for all Beans. SimpleBean
sets 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 JDK Tool Reference Page provides complete CLASSPATH information for
both Windows
and Solaris
platforms.
-
Compile the Bean:
javac SimpleBean.java
This produces the class file SimpleBean.class
-
Create a manifest file. Use your favorite text editor to create
a file, we'll call it manifest.tmp, that contains the following
text:
Name: SimpleBean.class
Java-Bean: True
-
Create the JAR file. The JAR file will contain the manifest and
the SimpleBean class file:
jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
See the JAR
File Format 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 ring up a file browser. Navigate to the SimpleBean.jar
location and select it. SimpleBean will appear at the bottom of
the ToolBox. (Note that when the BeanBox is started, all Beans in JAR files
in the beans/jars directory are automatically loaded into the
ToolBox).
-
Drop a SimpleBean instance into the BeanBox. Click on the
word SimpleBean in 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
that SimpleBean is selected. The SimpleBean properties
will appear in the Properties sheet.
You can resize SimpleBean, because it inherits from Canvas,
by dragging a corner. You will see the cursor change to a right angle when
over a corner. You can also reposition SimpleBean within 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 create SimpleBean.
# 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 Windows nmake
version:
# 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 the beans/demo directory,
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. With SimpleBean
selected, the Properties sheet displays four propeties: foreground,
background,
font,
and name. We declared no properties in SimpleBean, so
these are properties inherited from Canvas. Clicking on each property
brings up a property editor. The BeanBox provides default property editors
for the primitive types, plus Font and
Color types. You
can find the sources for these property editors in beans/apis/sun/beans/editors.
Beans communicate with other Beans by sending and receiving event notifications.
To see which events SimpleBean can 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.
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 the Edit|Report
menu item. The report lists Bean events, properties, and methods, and their
characteristics.
By default Bean reports are sent to the java interpreter'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
in beanbox/run.sh or run.bat to: java sun.beanbox.BeanBoxFrame
> beanreport.txt
REFERENCES:
1. JavaBean
concepts
2. BeanBox
Tutorial
3. Starting
and Using the BeanBox
4. Writing
a SimpleBean
5. DOWNLOADING
the BDK 1.0
