JavaBeans

Elisha Peterson

Abstract: This describes "JavaBeans" and how to use them. A Java "bean" is essentially a class that adopts a kind of contract regarding the names and nature of its methods. This contract allows the class to be easily handled when it comes to adjusting its properties dynamically or through property sheets, serializing (i.e. saving the class to a file and re-loading it), event handling, and many other situations. See also this official guide.


Properties are single data values that have get and set methods. Beans contain several properties. The JavaBeans specification consists of design patterns that allow beans to be made easily portable. While a single property might use ChangeEvents to notify listeners when a value has changed, a bean may use PropertyChangeEvents to notify listeners of changes to a specific property. This event contains additional information about the old and new values of a property.

The basic requirements for a Java "bean" are:

  • must implement the Serializable interface;
  • must have a no-argument constructor.

Introspection allows properties and event handling routines that are specified by particular code templates to be "discovered" by external classes. In particular, the Netbeans GUI builder can "detect" the bean properties and allow for customization. For properties of the standard types (boolean, short, int, long, float, double, String, Color), property editing is automatically supported within the GUI's property sheet. The code generated by the builder first calls the no-argument constructor, and then calls various set methods for the properties that have been customized.

Code Patterns

Besides the no-argument constructor, JavaBeans use several other code patterns.

Properties

To allow introspection, properties should conform to get/set patterns:

public C getXyz();
public void setXyz(C newValue);

where C is the class type and Xyz is the name of the property, e.g. "Value". The lowercase version "value" is the official property identifier. Note: for boolean properties, the getter may take the form public boolean isXyz();.

Indexed properties, typically arrays, support indexed get/set patterns also:

C[] getXyz()
void setXyz(C[] array)
C getXyz(int index)
void setXyz(int index, C element)

Events

Similarly, event handling should conform to add/remove patterns:

public void addXyzListener(XyzListener l)
public void removeXyzListener(XyzListener l)

where Xyz is replaced by the type of listener… e.g. ChangeListener or ActionListener.

Bound Properties

Bound properties fire PropertyChangeEvents when their value changes. This event includes (i) the String name of the property, (ii) the old value, and (iii) the new value. Any registered PropertyChangeListeners may respond to the change accordingly. The java.beans.PropertyChangeSupport class contains the necessary event handling. A typical set method differs for bound properties, as in the following example:

public void setTitle( String title )
{
    if ( ! this.title.equals(title) ) {
        String old = this.title;
        this.title = title;
        firePropertyChange( "title", old, title );
    }
}

Constrained Properties

Constrained properties fire VetoableChangeEvents when their value changes.

Persistence

Persistence refers to the ability to save and reload classes. JavaBean code patterns allow for automated persistence.

Property Editors

PropertyEditors are intended to "provide support for GUIs that want to allow users to edit a property value of a given type". In doing so, they handle PropertyChangeEvents for underlying beans. When the value is changed, the class fires an event notifying any registered listeners of the change.

The PropertyEditor API encapsulates a single bean, with getValue and setValue methods, and PropertyChangeEvent handling.

The API also includes getJavaInitializationString, which provides Java constructor code to get the value underlying the component. This can be used by GUI builders to automatically generate the underlying object, or a copy of that object. I'm not entirely sure about its purpose.

Display and Editing Options

There are three display options:

  1. Textual display, via getAsText.
  2. Combobox display, via getAsText and String[] getTags.
  3. Custom component display, via isPaintable and paintRectangle.

Similarly, there are three editing options:

  1. Textual editing, via setAsText.
  2. Combobox editing, via setAsText and String[] getTags.
  3. Custom component editing, via supportsCustomEditor and Component getCustomEditor.

Standard Support Class

The default implementation is the PropertyEditorSupport class, which has simple text editing support.

  • Allows customizing the source of PropertyChangeEvents: this defaults to the class, but also may be set directly via the "source" bean property.
  • Supports getAsText and setAsText, but an exception is thrown in the set case unless the value is a String.
  • Does not support custom editors or tags.

Package Implementations

Several editors are already included within the sun.beans.editors package. Most of these support textual editing only. Exceptions are noted.

  • BooleanEditor (tags)
  • ByteEditor
  • ColorEditor (panel)
  • DoubleEditor
  • EnumEditor (tags)
  • FloatEditor
  • FontEditor (panel)
  • IntegerEditor
  • LongEditor
  • NumberEditor
  • ShortEditor
  • StringEditor

Customizers

The Customizer interface has basic PropertyChangeEvent support, as well as a setObject(Object bean) method that sets the argument based on the value within the customizer. These are intended to provide for more advanced customization capabilities.


Please up-vote this page if you like its contents!

rating: 0+x

Leave a comment or a question below:

Page tags: java javabeans
page_revision: 7, last_edited: 1258892760|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License