Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

JavaBeans 101

 
Online Training Index

101, Part I

Introduction | Page 2 | Page 3 | Page 4

For your first JavaBean, let's look at the BDK BeanBox. Although in this tutorial we use the BeanBox to create a JavaBean, the BeanBox is generally used to test your JavaBeans. The BeanBox is considered a reference builder tool environment. It is not designed for building GUI applications, nor is it meant to have a look and feel of such other builder tools as Visual Age, Delphi, or Visual Basic.

You can create a JavaBean and then use the BeanBox to test that it runs properly. If a JavaBean runs properly in the BeanBox, you can be sure that it works properly with other commercial builder tools.

Starting the BeanBox

When you start the BeanBox, you'll see three windows:

  • ToolBox window
  • BeanBox window
  • Properties window

The ToolBox window displays the JavaBeans that are currently installed in the BeanBox, such as the Beans the come with the BeanBox demo. When the BeanBox starts, it automatically loads its ToolBox with the Beans in the JAR files contained in the bean/jars directory. You can add additional Beans, such as your own Beans, to the ToolBox. The next lesson, Writing a Simple JavaBean, explains how to add Beans to the ToolBox.

The BeanBox window itself appears initially as an empty window. You use this empty window, sometimes referred to as a "form" by other builder tools, for building applications.

The third window, the Properties window, displays the current properties for the selected Bean. If no Bean is selected, such as when you first start the BeanBox or if you click in the BeanBox window's background, then the Properties window displays the BeanBox properties. You can use the Properties window or sheet to edit a Bean's properties.

Using the BDK BeanBox

Using the Demo BDK JavaBeans

The easiest way to understand how the BeanBox works is to use it. The BeanBox enables you to construct simple Beans applications without writing any Java code. As a first example, you can build a simple "Juggling Duke" application in which Duke will start or stop juggling depending on which of two buttons you push.

In this lesson, you'll learn how to:

  • Drop Beans from the ToolBox into the BeanBox and change their properties using the Properties sheet and associated property editors.

  • Have one Bean fire an event and another Bean react to the fired event.

juggler-preview.gif

Back to Top

Creating your Bean

We'll start by dropping the Juggler Bean from the ToolBox into an empty BeanBox.

  1. Click on Juggler Bean to select from the list of Beans in the Toolbox window.

     

    menu-toolBox-juggler.gif

    Notice that the cursor changes to a crosshair.

  2. Place the cursor anywhere in the BeanBox, then click the mouse.

    This inserts a Juggler Bean into the BeanBox window. The highlighted box surrounding the Juggler indicates the Juggler is the currently selected bean.

     

    images/juggler-placed-on-form.gif

    Next we'll look at adding a start button to control the Juggler. This button Bean is an instance of the OurButton Bean class.

     

    menu-toolBox-ourButton.gif

  3. Click the OurButton Bean name in the ToolBox, then place an instance of the button in the BeanBox. Select the button in the BeanBox so that the button's properties display in the Property sheet.
  4. Edit the label field in the button's Property sheet so that the button's label reads "start."

    property-sheet-start-button.gif

    Notice that the text for the button in the BeanBox changes from "press" to "start" after you type "start" into the label field of the property sheet editor.

  5. Use the BeanBox Edit menu to select an action event to be fired by the start button. Before choosing the event action, be sure that you have selected the start button.

    Back to Top

     

    Notice that once you select the actionPerformed menu item, BeanBox enters a state where a line emanates from the start button and follows the mouse as you move it around the window. This indicates that the button is the selected source for the action event, and that your next mouse press should be over the target Bean which defines appropriate event-handler methods, in this case the Juggler Bean.

     

    drag-line-startbut2-jug.gif

  6. Drag the line from the start button and release it over the Juggler Bean. A dialog appears listing applicable event handlers defined by the Juggler Bean.

  7. Select the startJuggling method as the target for the event, then press OK.
  8. Now, when you press the start button Duke should start tossing beans around in a circle over his head like a professional juggler.

    You can control Duke's juggling speed by manually setting the property value labeled animationRate in the Juggler's property sheet editor. For the appropriate property sheet editor to appear, the Juggler must be the currently selected Bean within the BeanBox frame.

 

property-sheet-juggler.gif

Completing your Program

To complete the example program, add a stop button. Repeat the steps you took when you added the start button and connected it to the appropriate Juggler action.

  1. Select OurButton from the list of available Beans in the Toolbox menu, then drop it below the start button in the BeanBox.

    menu-toolBox-ourButton.gif

    When the new button is the currently selected Bean, the appropriate property editor appears.


  2. Edit the label field of the property sheet to read "stop."

  3. Hook up the action event from the stop button to the Juggler's stopJuggling event-handler method. Make sure the stop button is the currently selected bean.

  4. Select the actionPerformed event from the Edit/Events menu.

  5. Drag the event line from the stop button source to the Juggler Bean target. Press and release the mouse button with the connection line over the Juggler.

  6. The dialog of applicable event-handler methods defined for the Juggler Bean displays; select the stopJuggling event and click OK.

You should now be able to start and stop the juggler by pressing the appropriate button.

Summary

You've learned how to select Beans from the ToolBox and place them in the BeanBox. Once in the BeanBox, you can use the Bean's Property sheet and property editors to change its appearance. You can also use the BeanBox menu options to connect events from one Bean to another, to initiate and control Bean actions.

Keep in mind that the BeanBox is not an application builder tool. Its main purpose is to test Beans and verify that introspection and event hookup works properly. You would not really use the BeanBox to build and run an application.

Instead, think of the BeanBox as a reference builder tool. If your bean runs properly in the BeanBox, and it reports the proper events that you want your bean to fire as well as the proper event handlers for events you want it to respond to, you can be confident that your bean will work properly in other Beans-enabled builder tools.

 

Introduction | Page 2 | Page 3 | Page 4

Back to Top

Watch for JavaBeans 101, Part II in November