The Bean-Writing Process


Most of the rest of this chapter shows you the techniques that you use to write beans. Before we go into details, we give an overview of the process. First, we want to stress that writing a bean is not technically difficultthere are only a few new classes and interfaces for you to master.

In particular, the simplest kind of bean is really nothing more than a Java class that follows some fairly strict naming conventions for its methods.

NOTE

Some authors claim that a bean must have a default constructor. The JavaBeans specification is actually silent on this issue. However, most builder tools require a default constructor for each bean, so that they can instantiate beans without construction parameters.


Example 8-1 at the end of this section shows the code for an ImageViewer bean that could give a Java builder environment the same functionality as the Visual Basic image control we mentioned in the previous section. When you look at this code, notice that the ImageViewerBean class really doesn't look any different from any other class. For example, all accessor methods begin with get, and all mutator methods begin with set. As you will soon see, builder tools use this standard naming convention to discover properties. For example, fileName is a property of this bean because it has get and set methods.

Note that a property is not the same as an instance field. In this particular example, the fileName property is computed from the file instance field. Properties are conceptually at a higher level than instance fieldsthey are features of the interface, whereas instance fields belong to the implementation of the class.

One point that you need to keep in mind when you read through the examples in this chapter is that real-world beans are much more elaborate and tedious to code than our brief examples, for two reasons.

  1. Beans must be usable by less-than-expert programmers. You need to expose lots of properties so that your users can access most of the functionality of your bean with a visual design tool and without programming.

  2. The same bean must be usable in a wide variety of contexts. Both the behavior and the appearance of your bean must be customizable. Again, this means exposing lots of properties.

A good example of a bean with rich behavior is CalendarBean by Kai Tödter (see Figure 8-2). The bean and its source code are freely available from http://www.toedter.com/en/jcalendar. This bean gives users a convenient way of entering dates, simply by locating them in a calendar display. This is obviously pretty complex and not something one would want to program from scratch. By using a bean such as this one, you can take advantage of the work of others, simply by dropping the bean into a builder tool.

Figure 8-2. A calendar bean


Fortunately, you need to master only a small number of concepts to write beans with a rich set of behaviors. The example beans in this chapter, although not trivial, are kept simple enough to illustrate the necessary concepts.

Example 8-1. ImageViewerBean.java
  1. package com.horstmann.corejava;  2.  3. import java.awt.*;  4. import java.io.*;  5. import javax.imageio.*;  6. import javax.swing.*;  7.  8. /**  9.    A bean for viewing an image. 10. */ 11. public class ImageViewerBean extends JLabel 12. { 13. 14.    public ImageViewerBean() 15.    { 16.       setBorder(BorderFactory.createEtchedBorder()); 17.    } 18. 19.    /** 20.       Sets the fileName property. 21.       @param fileName the image file name 22.    */ 23.    public void setFileName(String fileName) 24.    { 25.       try 26.       { 27.          file = new File(fileName); 28.          setIcon(new ImageIcon(ImageIO.read(file))); 29.       } 30.       catch (IOException e) 31.       { 32.          file = null; 33.          setIcon(null); 34.       } 35.    } 36. 37.    /** 38.       Gets the fileName property. 39.       @return the image file name 40.    */ 41.    public String getFileName() 42.    { 43.       if (file == null) return null; 44.       else return file.getPath(); 45.    } 46. 47.    public Dimension getPreferredSize() 48.    { 49.       return new Dimension(XPREFSIZE, YPREFSIZE); 50.    } 51. 52.    private File file = null; 53.    private static final int XPREFSIZE = 200; 54.    private static final int YPREFSIZE = 200; 55. } 



    Core JavaT 2 Volume II - Advanced Features
    Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)
    ISBN: 193164411X
    EAN: 2147483647
    Year: 2003
    Pages: 156
    Authors: Jim Hoskins

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