BeanBox example beans

[source: http://java.sun.com/beans/software/examples.html]

The BDK provides a number of example beans that demonstrate various aspects of the JavaBeans architecture. Their sources are in the "demo" directory.

In addition, color-annotated source code for ExplicitButton, JellyBean, Juggler, Molecule, and OurButton is used to illustrate various Beans topics.

  1. BridgeTester
  2. ChangeReporter
  3. ExplicitButton (*Customization)
  4. EventMonitor
  5. JDBC SELECT
  6. JellyBean (*Properties)
  7. Juggler (*Persistence/Serialization)
  8. Molecule (*Custom Property Editor)
  9. OrangeButton
  10. OurButton (*Events)
  11. QuoteMonitor
  12. SortItem
  13. TickTock
  14. TransitionalBean
  15. Voter

1.   BridgeTester

The BridgeTester is a test bean that supports a wide range of different property types.

2.   ChangeReporter

The ChangeReporter is a small text window that can be used to display changes to bound properties.

Try connecting up the PropertyChange event from (say) an OurButton to the reportChange method of a ChangeReporter bean. Then when you change a bound property on the OurButton, the ChangeReporter will report the change.

This provides a simple way of checking that your bean is correctly firing PropertyChange events from its bound properties.

3.   ExplicitButton

ExplicitButton is a subclass of OurButton that adds no new functionality, but simply adds a BeanInfo class to specify the bean's behavior and icons. Color-annotated source code illustrates how this BeanInfo class is used to expose specific properties and events.

When you compare an ExplicitButton with an OurButton, you will see that the ExplicitButton has a smaller set of properties and a smaller set of better named events in the Edit/Events sub-menu. The ExplicitButton also has an icon in the ToolBox; the monochrome icon shown is returned by ExplicitButtonBeanInfo when the ToolBox requests a color icon.

The ExplicitButton also comes with a trivial customizer, the ExplicitButtonCustomizer that simply lets you change the label for the button. (This is not a particularly good use of a customizer!)

The ExplicitButton also indicates that it has some hidden-state (which in this case is a lie!) by using the FeatureDescriptor's attribute/value mechanism to specify in the bean's BeanDescriptor an attribute named "hidden-state" to be Boolean true. Following the JavaBeans 1.01 API specification, code generation cannot be done in this case and serialization must be used instead.

4.   EventMonitor

The EventMonitor bean allows you to monitor all the events fired by a source bean. It does this by analyzing the source bean and creating event adaptors for all the different kinds of events fired by the bean.

The EventMonitor sources provide an example of how a sophisticated bean, such as a bean container, can dynamically generate code to handle arbitrary kinds of events from other beans.

To use the EventMonitor bean you must:


As soon as that first event is delivered from the source bean to the EventMonitor, the EventMonitor will leap like a Tasmanian Devil on the source bean, analyze it, discover all the different events it fires, and create and register an event listener for each type of event. It will then report whenever any of these events are fired.

You may want to resize the EventMonitor bean to see all the different event output.

5.   JDBC SELECT

The JDBC SELECT bean contacts a target database at a specified JDBC URL and runs a SQL SELECT statement to display specified columns from a target table. You need to have JDBC access to a true SQL database (such as Oracle or Microsoft SQL Server) to use this bean.

Because the JDBC SELECT bean is fairly complex to configure, it comes with its own customizer that helps you to configure the bean.

After dropping the JDBC SELECT bean onto the middle composition window, select the Edit/Customize... menu item to bring up the bean customizer.

This prompts you initially for a target database URL and a login name and password. Then you can use the customizer to view the available database tables (push either "User tables" or "All Tables") and select a target table. The customizer then shows the various columns in that target table and lets you chose which columns you want displayed in the bean. As you chose columns, the bean is updated to show the current output of the corresponding SQL SELECT statement.

You can connect up an OurButton Action event to call the SELECT bean's "update" method, so that you can cause the bean to re-execute its SELECT.

6.   JellyBean

JellyBean is a very simple bean. It simply draws an oval and supports a bound property "color" and a constrained property "priceInCents". The JellyBean provides mono and color icons in 16x16 and 32x32; the 16x16 color icon is displayed in the ToolBox.

Initially you will be able to edit the JellyBean's priceInCents field without difficulty. However if you connect up the JellyBean's vetoableChange event to a Voter bean's vetoableChange handler method, then when you try to change the JellyBean's price, the constrained property change will be rejected. Color-annotated source code describes how to implement constrained and bound properties.

7.   OrangeButton

OrangeButton is a bean that is just like ExplicitButton except that its default color is Orange.

It is interesting because it's implemented as a serialized object, which was formed by taking a regular ExplicitButton, changing its background color and serializing it to a file. See the creating program in OrangeButtonWriter.java

8.   Juggler

The Juggler bean runs a very simple animation.

You can start and stop the animation by connecting the button push events on two ExplicitButtons to the startJuggling and stopJuggling methods on a Juggler.

Color-annotated source code for Juggler illustrates Serialization/Persistence.

The Juggler has a very simple JugglerBeanInfo class that defines a 16x16 color icon for the Juggler, that gets displayed in the ToolBox.

9.   Molecule

The Molecule bean displays a 3-D representation of a molecule. You can rotate the molecule by clicking on the bean and dragging the mouse.

You can also rotate the molecule by connecting up buttons to the rotateX and rotateY methods of the Molecule.

The Molecule comes with its own MoleculeNameEditor custom Property Editor for the "moleculeName" property. This Property Editor lets you chose from one of 6 different simple molecules to display in the Molecule bean. The molecule descriptions are read from descriptor files that the Molecule bean load as class-name relative resources. Color-annotated source code illustrates how to implement this Property Editor.

10.   OurButton

The OurButton bean is a subclass of java.awt.Canvas that acts as a simple GUI button. Clicking on the button will cause it to fire a standard AWT actionPerformed event.

OurButton exposes the standard java.awt.Component properties (foreground, background, font, and name) and adds four of its own (label, fontSize, largeFont, and debug). Notice how the font related properties interact, so that if you change fontSize from 12 to 24, then the font and largeFont properties are also updated.

OurButton will dynamically resize itself whenever its label or font is changed, so that the text exactly fits in the bean.

Color-annotated source code for OurButton.java illustrates how to implement an event listener and an event source.

11.   QuoteMonitor

The QuoteMonitor bean uses Java Remote Method Invocation (RMI) to contact a remote quote server and displays either real or imaginary stock quote values.

To start a local RMI quote server that generates imaginary values for the QuoteMonitor to display, cd to the demo directory and if you are on Windows type "start nmake -f quote.mk run" or if you are on Unix type "gnumake -f quote.mk run &".

12.   SortItem

The SortItem is a serialized bean. It is created by the SortItemWriter.java program.

You can click on the sorter bean to start it sorting. You can change the sort algorithm in the PropertySheet to be any of "BubbleSort", "QSort", or "BidirBubbleSort".

If you're feeling like a little hacking, try changing the SortItem to have a custom property editor for its sort algorithm, like the Molecule does for its molecule name.

13.   TickTock

The TickTock bean is a very simple "invisible" bean. When you select it from the ToolBox and drop it onto the composition window it will come up as a small rectangle showing the bean name. You can hide this using the "hide invisible beans" item under the "view" menu.

TickTock fires a PropertyChange event at regular intervals. You can change the interval using the property sheet. Try connecting up the TickTock's PropertyChange event to a ChangeReporter to watch the events being fired.

14.   TransitionalBean

The Transitional bean is a very simple bean that follows the JDK 1.0.2 compatibility rules, so that it can run on JDK 1.0.2 as well as on JDK 1.1.

15.   Voter

The Voter is a simple bean that is designed to process vetoableChange events. By default it rejects all vetoableChange requests, though you can change its vetoAll property to "true" and it will allow changes.

If you connect up the vetoableChange events from a bean which supports constrained properties (such as the JellyBean with its priceInCents property) to a Voter bean then you can reject any proposed changes to constrained properties.

This allows you to test your bean's use of constrained properties.


Java, JavaBeans, and JavaSoft are trademarks of Sun Microsystems Inc.