Defining a Bean Customizer

A bean may want to provide some way for the user of a beanbox program to customize its properties other than by setting them one at a time. A bean can do this by creating a Customizer class for itself and registering the customizer class with the BeanDescriptor object returned by its BeanInfo class, as in Example 15-6.

A customizer must be some kind of GUI component that is suitable for display in a dialog box created by the beanbox. In addition, a customizer must implement the Customizer interface. This interface consists of methods for adding and removing property change event listeners and a setObject( ) method that the beanbox calls to tell the customizer what bean object it is customizing. Whenever the user makes a change to the bean through the customizer, the customizer sends a PropertyChangeEvent to any interested listeners. Finally, like a property editor, a customizer must have a no-argument constructor, so it can easily be instantiated by a beanbox.

Example 15-9 shows a customizer for our YesNoPanel bean. This customizer displays a panel that has the same layout as a YesNoPanel, but it substitutes a TextArea object for the message display and three TextField objects for the three buttons that the dialog can display. These text entry areas allow the user to enter values for the messageText, yesLabel, noLabel, and cancelLabel properties. Figure 15-2 shows this customizer panel displayed within a dialog box created by a beanbox program. Again, ...

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.