Previous | Next | Trail Map | JavaBeans Tutorial | Table of Contents


BeanBox Basics

The BeanBox is an application for testing Beans, and serves as a simple demonstration for how more sophisticated Beans-aware builder tools should behave. The BeanBox is also a nice tool for learning about Beans. You will use the BeanBox to help you understand Bean characteristics.

Starting and Using the BeanBox explains how to start the BeanBox, and describes the ToolBox and Properties sheet.

Creating a Minimal Bean walks you through creating a rudimentary Bean, saving the Bean, adding the Bean to the ToolBox, placing the Bean in the BeanBox, inspecting the Bean's properties and events, and generating a Bean introspection report.

The BeanBox Menus explains each item in the BeanBox File, Edit, and View menus.


Previous | Next | Trail Map | JavaBeans Tutorial | Table of Contents
Previous | Next | Trail Map | JavaBeans Tutorial | BeanBox Basics


Starting and Using the BeanBox

The beans/beanbox. directory contains Windows (run.bat) and Unix (run.sh) scripts that start the BeanBox. You can use these commands to start the BeanBox, or use make:

gnumake run
Or:
nmake run

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 running under CDE (Common Desktop Environment):

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. On Windows, it's a little tricky; you must click in the area where the hatched border would appear to select the Bean. On other systems clicking anywhere on the Bean will select 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. After the BeanBox is running you can load Beans Using the File|LoadJar... BeanBox menu item. Only Beans in the beans/jars directory will be loaded between startups though. 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

  1. Click on the word JellyBean in the ToolBox. The cursor will change to a crosshair.
  2. 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's 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 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:

  1. Drop a JellyBean on the BeanBox.
  2. Change the color property to anything you want.
  3. Select the File|Save BeanBox menu item. A file browser will pop up; use it to save the Bean to a file.
  4. Select the File|Clear BeanBox menu item.
  5. Select the File|Load BeanBox menu item. The file browser will again pop up; use it to retrieve the serialized Bean.

In the next lesson you learn more BeanBox capabilities by creating a simple Bean.


Previous | Next | Trail Map | JavaBeans Tutorial | BeanBox Basics
Previous | Next | Trail Map | JavaBeans Tutorial | BeanBox Basics


The BeanBox Menus

THIS SECTION IS UNDER CONSTRUCTION

This section explains each item in the BeanBox File, Edit, and View menus.

File Menu
Save
Saves the Beans in the BeanBox, including each Bean's size, position, and internal state. The saved file can be loaded via File|Load.
SerializeComponent...
Saves the Beans in the BeanBox to a serialized (.ser) file. This file must be put in a .jar file to be useable.
MakeApplet...
Generates an applet from the BeanBox contents.
Load...
Loads Saved files into the BeanBox. Will not load .ser files.
LoadJar...
Loads a Jar file's contents into the ToolBox.
Print
Prints the BeanBox contents.
Clear
Removes the BeanBox contents.
Exit
Quits the BeanBox without offering to save.

Edit Menu
Cut
Removes the Bean selected in the BeanBox. The cut Bean is serialized, and can then be pasted.
Copy
Copies the Bean selected in the BeanBox. The copied Bean is serialized, and can then be pasted.
Paste
Drops the last cut or copied Bean into the BeanBox.
Report...
Generates an introspection report on the selected Bean.
Events
Lists the event-firing methods, grouped by interface.
Bind property...
Lists all the bound property methods of the selected Bean.

View Menu
Disable Design Mode
Removes the ToolBox and the Properties sheet from the screen. Eliminates all beanBox design and test behavior (selected Bean, etc.), and makes the BeanBox behave like an application.
Hide Invisible Beans
Hides invisible Beans.


Previous | Next | Trail Map | JavaBeans Tutorial | BeanBox Basics
Previous | Next | Trail Map | JavaBeans Tutorial | BeanBox Basics


Creating a Minimal Bean

In this section you will continue learning some BeanBox fundamentals by

Your example Bean will be named SimpleBean, and you will create it in the beans/demo/sunw/demo/simple directory:

  1. Create the directory beans/demo/sunw/demo/simple. This will put SimpleBean in with the other BDK demo Beans.

  2. Write the SimpleBean code. Since you're new to Beans, we'll give it to you:
       
    package sunw.demo.simple;
    
    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. Setting the background color and component size is all that SimpleBean does.

  3. Write the makefile. Makefiles for Unix (gnumake) and Windows (nmake) are listed at the end of this section. These makefiles compile SimpleBean, and generate a JAR file in beans/jars (where the other demo jars reside). Save one of these makefiles as simple.gmk (Unix) or simple.mk (Windows), and put it in beans/demo.

  4. Run make from the beans/demos directory. For Unix, run
         gnumake simple.gmk
         
    For Windows, run
         nmake -f simple.mk
         
    This will compile SimpleBean, and create a JAR file in the beans/jars directory. The BeanBox looks in that directory for JAR files.

  5. Load the JAR file into the ToolBox. Pull down the File|LoadJar... menu item. This will bring up a file browser. Navigate to beans/jars and select simple.jar. 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.

  6. Drop an instance of SimpleBean into the BeanBox. Click on the word SimpleBean in the ToolBox. The cursor will change to crosshairs. Move the cursor to a spot within the BeanBox and click. SimpleBean will appear as a painted rectangle with 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 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.

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 (you will see how later), 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 interface, 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:

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

SimpleBean Makefiles

Here are two makefiles (Unix and Windows) set up to create SimpleBean. The makefiles compile the .java files, and create a JAR with the resulting .class file and a manifest.


# gnumake file CLASSFILES= \ sunw/demo/simple/SimpleBean.class JARFILE= ../jars/SimpleBean.jar all: $(JARFILE) # Create a JAR file with a suitable manifest. $(JARFILE): $(CLASSFILES) $(DATAFILES) echo "Name: sunw/demo/simple/SimpleBean.class" >> manifest.tmp echo "Java-Bean: True" >> manifest.tmp jar cfm $(JARFILE) manifest.tmp sunw/demo/simple/*.class @/bin/rm manifest.tmp # Compile the sources %.class: %.java export CLASSPATH; CLASSPATH=. ; \ javac $< # make clean clean: /bin/rm -f sunw/demo/simple/*.class /bin/rm -f $(JARFILE)

Here is the Windows nmake version:


# nmake file CLASSFILES= \ sunw\demo\simple\simplebean.class \ JARFILE= ..\jars\simplebean.jar all: $(JARFILE) # Create a JAR file with a suitable manifest. $(JARFILE): $(CLASSFILES) $(DATAFILES) jar cfm $(JARFILE) << manifest.tmp sunw\demo\simple\*.class Name: sunw/demo/simple/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 JavaSoft website contains documentation on JAR files.


Previous | Next | Trail Map | JavaBeans Tutorial | BeanBox Basics