Defining a Simple Property Editor

A bean can also provide auxiliary PropertyEditor classes for use by a beanbox tool. PropertyEditor is a flexible interface that allows a bean to tell a beanbox how to display and edit the values of certain types of properties.

A beanbox tool always provides simple property editors for common property types, such as strings, numbers, fonts, and colors. If your bean has a property of a nonstandard type, however, you should register a property editor for that type. The easiest way to “register” a property editor is through a simple naming convention. If your type is defined by the class X, the editor for it should be defined in the class X Editor. Alternatively, you can register a property editor by calling the PropertyEditorManager.registerEditor( ) method, probably from the constructor of your BeanInfo class. If you call this method from the bean itself, the bean then depends on the property editor class, so the editor has to be bundled with the bean in applications, which is not desirable. Another way to register a property editor is by using a PropertyDescriptor object in a BeanInfo class to specify the PropertyEditor for a specific property. The YesNoPanelBeanInfo class does this for the messageText property, for example.

The PropertyEditor interface can seem confusing at first. Its methods allow you to define three techniques for displaying the value of a property and two techniques for allowing the user to edit the value of a property. ...

Get Java Examples in a Nutshell, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.