Recipe 23.8 Preparing a Class as a JavaBean


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


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


Three kinds of Java components are called JavaBeans:

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

  • Components used in JavaServer Pages (JSPs).

  • Enterprise JavaBeans (EJBs), containing features for building enterprise-scale applications. Creating and using EJBs is more involved than regular JavaBeans and would take us very far afield, so EJBs are 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 technique is useful even in nonbean 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

  • 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 Recipe 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. A test program in the online source directory sets up three LabelText widgets, as shown in Figure 23-6.

Figure 23-6. 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, in addition to being a good example of a bean.

Example 23-4.
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 {     /** The label component */     protected JLabel theLabel;     /** The text field 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!

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: