JavaBean Demo - Shows how to make and use simple Java Beans

A Java Bean (or just Bean) is a regular Java class that adheres to the JavaBean specifications.  In addition, a Bean may contain various support classes.  Beans are components that may be added to an application using some sort of GUI tool.  These tools have ways of hooking up the events of one Bean component to the methods of another, and to customize the Bean's properties.

A Bean that has been customized is not a class, but a serialized object of that class with the extension .ser.  Because of this it is not sufficient to say:

myBean foo = new myBean(); to create a Bean in your Java code. 

All Beans are stored in jar files, with special manifest.mf file entries.  Beans are usually placed in individual packages, although several such Beans may be contained in a single jar.  Note that the jar file containing a serialized Bean must include the class file as well.  The manifest file must have entries for both files (foo.class and foo.ser). Beans may be used in a GUI development environment, such as the BDK BeanBox, or directly from other Java code.

SimpleBean.java is a trivial Bean that is a colored rectangle, with a single property myColor that can be set.  To create this Bean, you must first compile it in the usual manner, then create a manifest file with the correct entries.  The enties in your manifest file will be merged with the existing manifest file entries.  (The name of your manifest file doesn't matter; in the example below I used SimpleBean.mf.)  Finally the Bean class and manifest must be put into a jar.  That's it!

C:\Temp>dir

 Volume in drive C is WIN95
 Volume Serial Number is 3241-11C0
 Directory of C:\Temp

.              <DIR>        04-15-00 11:27a .
..             <DIR>        04-15-00 11:27a ..
SIMPLE~1   JAV         270  04-15-00 11:35a SimpleBean.java
SIMPLE~2   CLA         353  04-15-00 11:38a SimpleBean.mf
         2 file(s)            611 bytes
         3 dir(s)      82,575,360 bytes free

C:\Temp> javac -d . SimpleBean.java

C:\Temp> jar -cvfm SimpleBean.jar SimpleBean.mf mybean/*
added manifest
adding: mybeans/SimpleBean.class(in = 997) (out= 575)(deflated 42%)

C:\Temp>

Sample DOS session showing the steps in SimpleBean creation



To test this bean, compile and run BeanTest.java (which is both an Applet and a stand-alone Java program) as follows:
C:\Temp> javac -classpath SimpleBean.jar BeanTest.java
C:\Temp> java -classpath .;SimpleBean.jar BeanTest

A Java Bean may be customized.  This can be done by creating a Bean object, changing the instance variables of that object, and saving the object as a file.  This process is called serialization.  You can use the BeanBox from the BDK to easily customize a Bean's properties and to serialize that Bean to disk.  (Be sure to save the .ser file to the correct directory!)

The serialized Bean can then be added to the Bean's jar file.  The manifest must also be updated to show both the serialized Bean and the Bean's class.  (Both are marked as Java Beans.)  Use the jar tool to update the Bean jar file with the serialized Bean, which should have the filename .ser.

C:\Temp> /jdk/beans/beanbox/run       runs the BeanBox tool
C:\Temp> jar -uvfm SimpleBean.jar SimpleBean-ser.mf mybean/*.ser
updated manifest
adding: mybeans/SimpleBean.ser(in = 1694) (out= 1000)(deflated 40%)

C:\Temp>
Sample DOS session showing the steps in SimpleBean customization

Note that once a Bean has been customized, all of its proerties including inherited properites are also serialized.  So if you compile your Bean with Java 2 tools, then the customized Bean will require Java 2 to run.  (An uncustomized Bean will run fine in the Browser's Java, currently version 1.1 for most Browsers, provided you don't use any Java 2 only features.)  You can use the HTMLConverter from Sun to convert an HTML file's APPLET tags into some JavaScript that will run the Applet using the Java 2 Plug-in.

SimpleBean by default is white, but has been customized to be orange.  The result is shown below.