Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Soup up your Java classes with customization

Wow your beans users with cool customization features

  • 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
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources