Example Beans

As part of the BDK we include a number of example beans that demonstrate various aspects of the JavaBeans architecture.

The following beans are all present in the default ToolBox palette for the BeanBox. Their sources are in the "demo" directory, except for the BeanBox bean, whose source is in the "beanbox" directory.

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

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

  1. BeanBox

  2. This bean can act as a containers for other beans. This BeanBox bean is the central component of the BeanBox tool.

    When you drop a BeanBox inside of a BeanBox, the nested bean is given a darker background color to make it easier to see.

    When you select a nested BeanBox bean, its parent BeanBox gives it control of the MenuBar. Note that while you have a BeanBox selected, any beans that you choose off the ToolBox window must be dropped into the active BeanBox, not onto its parent.

  3. BridgeTester

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

  5. ChangeReporter

  6. 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.

  7. ExplicitButton

  8. ExplicitButton is a subclass of OurButton that adds no new functionality, but simply adds a BeanInfo class to specify the bean's behaviour 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 OurButtonCustomizer that simply lets you change the label for the button. (This is not a particularly good use of a customizer!)

  9. EventMonitor

  10. 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.

  11. JDBC SELECT

  12. 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.

  13. JellyBean

  14. 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.

  15. OrangeButton

  16. 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

  17. Juggler

  18. 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.

  19. Molecule

  20. 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.

  21. OurButton

  22. 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.

  23. QuoteMonitor

  24. 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 &".

  25. SorterBean

  26. The SorterBean is a serialized applet which can also run as a bean. It is created by the SorterBeanWriter.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 SorterBean to have a custom property editor for its sort algorithm, like the Molecule does for its molecule name.

  27. TickTock

  28. 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.

  29. TransitionalBean

  30. 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.

  31. Voter

  32. 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.

Copyright © 1996 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA 94043-1100 USA.
All rights reserved.