Example Beans

edited from the SUN page
  1. Juggler (*Persistence/Serialization)
  2. ChangeReporter
  3. ExplicitButton (*Customization)
  4. EventMonitor
  5. JellyBean (*Properties)
  6. Molecule (*Custom Property Editor)
  7. OurButton (*Events)
  8. TickTock(*Invisible)
  9. Voter(*Vetoable Change)

  1. Juggler

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

  3. ChangeReporter

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

  5. ExplicitButton

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

  7. EventMonitor

  8. 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. (Get one and see how it reacts to simply the OurButton - then see difference to ExplicitButton.)

    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.

  9. JellyBean

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

  11. Molecule

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

  13. OurButton

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

  15. TickTock

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

  17. Voter

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