Section 22.3. Properties and Customizers


22.3. Properties and Customizers

Properties represent the "state" or "data" content of a bean. These features can be manipulated externally to configure the bean. For a bean that's a GUI component, you might expect its properties to include its size, color, and other features of its basic appearance. Properties are similar in concept to an object's public variables. Like a variable, a property can be a primitive type (such as a number or Boolean), or it can be a complex object type (such as a String or a collection of spreadsheet data). Unlike variables, properties are always manipulated using methods to set and get the value; this enables a bean to take action whenever a property changes. By sending an event when a property changes, a bean can notify other interested beans of the change (as discussed later in this chapter).

Let's pull a couple of beans into NetBeans and take a look at their properties. Click on a JButton from the Swing palette, and then click in the workspace. When the JButton is first loaded by NetBeans, it was inspected to discover its properties. When we select an instance of the button, NetBeans displays these properties in the Properties pane and allows us to modify them.

The button has nine basic properties, followed by a few additional groupings. The list called Other Properties adds more detail. There is also a Layout properties section (these are in actuality not properties of the JButton, but are here for convenience) as well as an Accessibility properties list (for components that have accessibility-related properties). The foreground and background properties are colors; their current values are displayed in the corresponding box. font is the font for the label text; an example of the font is shown. text is the text of the button's label. You can also set an image icon for the button, the tooltip text that appears when the mouse hovers over the item, and a keyboard-shortcut identifier, called a mnemonic. Try typing something new for the value of the text property, and watch the button label change. Click on the background color to enter a numeric color value, or, better yet, press the "..." button to pop up a color-chooser dialog.

Most of these basic properties will be familiar to you because many GUI beans inherit them from the base JComponent class. The Other Properties section lists almost 50 additional properties inherited from JComponent. NetBeans is making an effort to categorize these for us. As we'll see when we create our own beans, we can limit which properties are included in the Properties pane.

Now place a Juggler bean in the workspace (this is one of Sun's original demonstration Java beans that we have updated). The animation starts, and the juggler begins juggling some roughly drawn beans, as shown in Figure 22-3. If he gets annoying, don't worry; we'll have him under our control soon enough.

You can see that this bean has a different set of properties, including an interesting one called animationRate. It is an integer property that controls the delay in milliseconds between displays of the juggler's frames. Try changing its value. The juggler changes speed as you type each value. Good beans give you immediate feedback when you change their properties. Uncheck the checkbox next to the Boolean juggling property to stop the show if you want.

Notice that the Properties pane provides a way to display and edit each of the different property types. For the foreground and background properties of the JButton, the pane displays the color; if you click on them, a color selection dialog pops up. Similarly, if you click on the font property, you get a font selection dialog. For integer and string values, you can type a new value into the field. NetBeans understands and can edit the most useful basic Java types.

Since the property types are open-ended, NetBeans can't possibly anticipate them all. Beans with more complex property types can supply their own property editor. The

Figure 22-3. Juggling beans


Molecule bean that we'll play with in the next section, for example, uses a custom property editor that lets us choose the type of molecule. If it needs even more control over how its properties are displayed, a bean can provide a customizer, which allows a bean to provide its own GUI for editing its properties. (For example, a customizer could let you draw an icon for a button.)



    Learning Java
    Learning Java
    ISBN: 0596008732
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net