A Java bean is thought of in terms of three components: its properties, methods, and events. It is easy to turn nearly any simple Java program into a bean — just implement getter and setter methods for the data objects which can be used with other beans: for instance, if a class has a property color, it should have accessor methods getColor() and setColor(). In order to allow beans to be useful in the context of the beanbox or a more advanced GUI tool, it is necessary to implement the methods of a property change listener and also add lines of code such as the following:
private PropertyChangeSupport changes = new PropertyChangeSupport(this);
...and in the setter method...
changes.firePropertyChange("color", oldColor, newColor);
This notifies the beanbox that the property has been changed. The property color can be viewed in the Property Editor which support simple types of properties like int, string, etc.. It is also possible to define one's own property editor in code for more advanced types. Furthermore the states of beans can be saved between sessions because a key feature of beans is their ability to be written to and read from disk using the JDK 1.1 Serializable interface.
In order to package a class (e.g. an applet) as a bean it is necessary to archive it in a jar file, using a manifest.stub file to indicate which classes are beans. The jar can then be loaded within the beanbox or put into a jar folder which the beanbox uses at startup time. With this release it can be a bit tricky ensuring that everything is in the correct folders and archived correctly. In particular I discovered that if the bean wishes to load resources from relative URLs it cannot use getCodeBase() in the normal fashion.
Below is a example of a simple bean I created called SpeedSwitch. By clicking on the graphical space the user can move the switch to one of four states: off, slow, medium, and fast. The bean can be selected and dragged from the Toolbox (it is at the bottom) and the state is shown in the Property Editor along with other properties.