Soup up your Java classes with customization
Wow your beans users with cool customization features
By Mark Johnson, JavaWorld.com, 12/09/97
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Those of you who have been following this column have already read my first two installments on Java customization. The first,"
Double Shot, Half Decaf, Skinny Latte: Customize your Java," was an introduction to the concept of object properties and the various ways in which beans can be customized. Last month's
column, "
The trick to controlling bean customization," covered the general concepts of bean customization structure, and was a bit light on code and examples. This month, we'll
hit some more complex coding examples. The background to JavaBeans customization has been covered in previous articles, so
this article will assume that you've either read those articles, or you have an equivalent understanding of the topic.
Because the more advanced JavaBean customization features are only necessary for more advanced beans, we'll start with a walk-through
of ScatterPlot, a bean that displays a scatter plot of data it retrieves from another object. The ScatterPlot class will (perhaps predictably) provide us with some gnarly customization problems. Fortunately, we'll have the tools at
hand to solve them, armed as we are with the customization interface of the JavaBeans API. We'll overcome some of the limitations
of property sheets by writing and registering a property editor for a derived type, and we'll finish by writing a complete
customizer class.
Property editors vs. customizers
To recap from last month's column, there's a difference between a property editor and a customizer. A property editor is a
class that implements the java.beans.PropertyEditor interface (although it often does so indirectly, by extending the "adapter" class java.beans.PropertyEditorSupport). Its purpose is to allow the user to edit a single property of a bean. The BeanBox container, from Sun, comes with several
built-in property editors that it presents to application developers in a property sheet, as follows. (For more on the BeanBox,
see my previous JavaWorld article, "The BeanBox: Sun's JavaBeans test container." Check-out the Resources section of this article to download the BeanBox.)
| Class |
Property Editor |
| Boolean |
BoolEditor |
| Byte |
ByteEditor |
| Color |
ColorEditor |
| Double |
DoubleEditor |
| Float |
FloatEditor |
| Font |
FontEditor |
| Int |
IntEditor |
| Long |
LongEditor |
| Number |
NumberEditor |
| Short |
ShortEditor |
| String |
StringEditor |
Built-in BeanBox property editors
When the user selects a bean in the BeanBox, the BeanBox application builds a custom property sheet for the bean. It first
introspects the bean to figure out what its properties are. Then, based on the class, the BeanBox places an instance of each
of these property editors, one per property, along with labels identifying the properties. Any properties for which it either
can't find or (for some reason) can't construct property editors are omitted from the property sheet. This means that any
properties for which no editor exists, including properties of types you've created, won't show up on the property sheet,
even if appropriate setter and getter methods have been written for them. (This is an important point, as we'll see later.)
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- You can download the source code for this article in Unix tar format (ScatterPlot.tar) or in ZIP format (ScatterPlot.zip).
- You can also download a JAR file for the code in this article (ScatterPlot.jar).
- There's a cool example of a couple of JavaBeans at http://ic.kol.de/beans/colorcube/. This bean is a sort of "color cube" that lets you set colors graphically. As an exercise, try using this class as a property
editor for colors in the BeanBox. (Note that this bean is available for download, but the owners reserve all rights to its
use.)
- If you want to see just how cool a customizer can be, check out the demo versions of the beans at http://www.protoview.com/default.htm . The installation process is somewhat manual, but it's worth the wait. The customization of these beans is awesome.
- A lighter-weight example of how to use customizers is the Water Beans page at http://www.geocities.com/SiliconValley/Lakes/3767/b-unlim.htm. Water Beans gives examples of how to use property editors, BeanInfo, a customizer, and serialization. They work in the BeanBox,
but documentation is sparse. Still, they're good examples.
- The creators of the WebRunner toolkit at Taligent have demo versions of their beans at http://www.taligent.com/Products/webrunner/resources/WRBeans.html. According the documentation, several of the beans have customizer classes. (Don't count on getting source code, though.)
A list of WebRunner products can be found at http://www.taligent.com/Products/webrunner/resources/WRProdIndex.html.
- There are some brief JavaBean tutorial examples on icons, descriptors, and property editors at http://www.powerup.com.au/~mgk/javabeans/index.html, and they're really quite nicely done. Hats off to Mark King for making them available on the Web.
- Download the BeanBox from the Sun Web site at http://splash.javasoft.com/beans/bdk_download.html.