Preparing a Class as a JavaBean

Problem

You have a class that you would like to install as a JavaBean.

Solution

Make sure the class meets the JavaBeans requirements; create a JAR file containing the class, a manifest, and any ancillary entries.

Discussion

There are three kinds of Java components that are called JavaBeans:

  • Visual components for use in GUI builders, as discussed in this chapter.

  • Components that are used in JavaServer Pages (JSP). Examples of these are given in Chapter 18.

  • Enterprise JavaBeans (EJB) has features for building enterprise-scale applications. Creating and using EJB is more involved than regular JavaBeans and would take us very far afield, so EJB is not covered in this book. When you need to learn about EJB functionality, turn to the O’Reilly book Enterprise JavaBeans.

What all three kinds of beans have in common are certain naming paradigms. All public properties should be accessible by get/set accessory methods. For a given property Prop of type Type, the following two methods should exist (note the capitalization):

public Type getProp(  );
public void setProp(Type)

For example, the various AWT and Swing components that have textual labels all have the following pair of methods:

public String getText(  );
public void setText(String newText);

You should use this set/get design pattern (set/get methods) for methods that control a bean. Indeed, this is useful even in non-bean classes for regularity. The “bean containers” -- the Bean Builders, the JSP mechanism, and the EJB mechanism -- all use Java introspection (see Chapter 25) to find the set/get method pairs, and some use these to construct properties editors for your bean. Bean-aware IDEs, for example, provide editors for all standard types (colors, fonts, labels, etc.). You can supplement this with a BeanInfo class to provide or override information.

The bare minimum a class requires to be usable as a JavaBean in a GUI Builder is the following:

  • The class must implement java.io.Serializable.

  • The class must have a no-argument constructor.

  • The class should use the set/get paradigm.

  • The class file should be packaged into a JAR file with the jar archiver program (see Section 23.9).

Here is a sample bean that may be a useful addition to your Java GUI toolbox, the LabelText widget. It combines a label and a one-line text field into a single unit, making it easier to compose GUI applications. There is a test program in the source directory that sets up three LabelText widgets, and is shown in Figure 23-5.

LabelText bean

Figure 23-5. LabelText bean

The code for LabelText is shown in Example 23-4. Notice that it is serializable and uses the set/get paradigm for most of its public methods. Most of the public set/get methods simply delegate to the corresponding methods in the label or the text field. There isn’t really a lot to this bean, but it’s a good example of aggregation, as well as being a good example of a bean.

Example 23-4. LabelText.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** A label and text combination, inspired by
 * the LabelText control in Guy Eddon's ActiveX Components book
 * (2nd Edition, p. 203). But done more, simply.
 * 
 */
public class LabelText extends JPanel implements java.io.Serializable {
    /** The label component */
    protected JLabel theLabel;
    /** The label component */
    protected JTextField theTextField;

    /** Construct the object with no initial values.
     * To be usable as a JavaBean there MUST be a no-argument constructor.
     */
    public LabelText(  ) {
        this("(LabelText)",  12);
    }

    /** Construct the object with the label and a default textfield size */
    public LabelText(String label) {
        this(label, 12);
    }

    /** Construct the object with given label and textfield size */
    public LabelText(String label, int numChars) {
        super(  );
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        theLabel = new JLabel(label);
        add(theLabel);
        theTextField = new JTextField(numChars);
        add(theTextField);
    }

    /** Get the label's horizontal alignment */
    public int getLabelAlignment(  ) {
        return theLabel.getHorizontalAlignment(  );
    }

    /** Set the label's horizontal alignment */
    public void setLabelAlignment(int align) {
        switch (align) {
        case JLabel.LEFT:
        case JLabel.CENTER:
        case JLabel.RIGHT:
            theLabel.setHorizontalAlignment(align);
            break;
        default:
            throw new IllegalArgumentException(
                "setLabelAlignment argument must be one of JLabel aligners");
        }
    }

    /** Get the text displayed in the text field */
    public String getText(  ) {
        return theTextField.getText(  );
    }

    /** Set the text displayed in the text field */
    public void setText(String text) {
        theTextField.setText(text);
    }

    /** Get the text displayed in the label */
    public String getLabel(  ) {
        return theLabel.getText(  );
    }

    /** Set the text displayed in the label */
    public void setLabel(String text) {
        theLabel.setText(text);
    }

    /** Set the font used in both subcomponents. */
    public void setFont(Font f) {
        theLabel.setFont(f);
        theTextField.setFont(f);
    }

    /** Adds the ActionListener to receive action events from the textfield */
    public void addActionListener(ActionListener l) {
        theTextField.addActionListener(l);
    }

    /** Remove an ActionListener from the textfield. */
    public void removeActionListener(ActionListener l) {
        theTextField.removeActionListener(l);
    }
}

Once it’s compiled, it’s ready to be pickled into a JAR. JavaBeans people really talk like that!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.33.235