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

SBean.java is a trivial Bean that is a colored rectangle, with a single property theColor 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.  Then the classes (including a BeanInfo class), the manifest file, and some .gif files (which will be used as icons for the Bean) are added to a jar file.  Later a serialized version of the bean (with a .ser extension) can be added to the jar, to save a customized verison of the bean.

The enties in your manifest file will be merged with the existing manifest file entries in the jar file.  (The name of your manifest file doesn't matter; in this example I used SBean.mf.) In this case there are entries in the manifest for the bean, a serialized version of the bean (which we don't have now but will add later when the bean is customized), and an entry specifiying the main entry for the jar file.  This last entry allows the jar to be double-clicked to run from the desktop.  Some of the optional entries in a manifest file include:

(Some of these items are per entry, not once per jar file.  See the JDK Documentation for jar for more details on the manifest file format.)

For this bean we wish to hide the inherited java.awt.Component properties and expose only theColor.  This can be done by including a BeanInfo class with the bean.  Without a BeanInfo class Java uses reflection to determine methods, events, and properties of a bean that can be seen at design time.  With a BeanInfo class we can pick and chose what to hide and what to expose in the development tool (e.g., BeanBox).  Using a BeanInfo class we can also associate an a PropertyEditor or Customizer with specific properties.  The BeanInfo class is named after the bean, in our case SBeanBeanInfo.

The BeanInfo class can also specify an icon to appear in the beanbox (or other design tool):  SBean Icon  (Its supposed to look like a bean.)

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 ..
SBEAN    HTM         4,096  04-22-00  5:08p SBean.htm
SBEAN~1  JAV           794  04-20-00  5:16p SBean.java
SBEAN    MF             98  04-20-00  5:25p SBean.mf
BEANTE~1 JAV         1,152  04-20-00  5:24p BeanTest.java
SBEANB~2 JAV         2,190  04-24-01  9:33a SBeanBeanInfo.java
SBEANI~1 GIF            82  04-20-00  5:19p SBeanIcon16.gif
SBEANI~2 GIF           881  04-20-00  5:18p SBeanIcon16c.gif
SBEANI~3 GIF           143  04-20-00  5:22p SBeanIcon32.gif
SBEANI~4 GIF           976  04-20-00  5:21p SBeanIcon32c.gif
         9 file(s)         10,412 bytes
         2 dir(s)        2,008.29 MB free

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

C:\Temp>jar -cvfm SBean.jar SBean.mf *.class *.gif
added manifest
adding: BeanTest$1.class(in = 377) (out= 268)(deflated 28%)
adding: BeanTest.class(in = 1523) (out= 875)(deflated 42%)
adding: SBean.class(in = 982) (out= 574)(deflated 41%)
adding: SBeanBeanInfo.class(in = 1928) (out= 1038)(deflated 46%)
adding: SBeanIcon16.gif(in = 82) (out= 77)(deflated 6%)
adding: SBeanIcon16c.gif(in = 881) (out= 533)(deflated 39%)
adding: SBeanIcon32.gif(in = 143) (out= 143)(deflated 0%)
adding: SBeanIcon32c.gif(in = 976) (out= 655)(deflated 32%)

C:\Temp>

Sample DOS session showing the steps in SBean creation.

Note the 4 versions of the icon.  (The BeanBox uses these without any Content-Type: manifest entries.)

Also note the addition of the BeanTest classes.  These are used to test the bean, by allowing the jar file to run as an application.



To test this bean, compile and run BeanTest.java (which is both an Applet and a stand-alone Java program).  This was done already as shown above, but in general you can add such a test main program/applet to an existing bean, and run it from the command line (could also double-click on the jar) as follows:

C:\Temp>javac -classpath SBean.jar BeanTest.java
C:\Temp>jar -uf SimpleBean.jar BeanTest.class BeanTest$1.class
C:\Temp>java -jar SBean.jar

The bean can be loaded into the BeanBox, customized (Say to lime green), serialized, and the .ser file added to the bean's jar using the jar -uf command.  If you do all that the resulting jar file can be added to a web page (since BeanTest is also an Applet) and should look something like this:




Send comments and mail to the WebMaster.