[ Team LiB ] Previous Section Next Section

Chapter 15. JavaBeans

The JavaBeans API provides a framework for defining reusable, embeddable, modular software components. The JavaBeans specification includes the following definition of a bean: "a reusable software component that can be manipulated visually in a builder tool." As you can see, this is a rather loose definition; beans can take a variety of forms. At the simplest level, individual GUI components are all beans, while at a much higher level, an embeddable spreadsheet application might also function as a bean. Most beans, however, probably fall somewhere between these two extremes.

Note that the definition of a Java bean explicitly supposes the existence of a visual GUI builder tool. In this chapter, we'll use the term "beanbox" to refer to any such tool. The name is from an obsolete JavaBeans demonstration tool shipped by Sun with the early releases of JavaBeans. A new tool, called "Bean Builder," has replaced the beanbox, and you can download it from http://java.sun.com/products/javabeans if you do not already have a bean-enabled development environment.

One of the goals of the JavaBeans model is interoperability with similar component frameworks. So, for example, a native Windows program can, with an appropriate bridge or wrapper object, use a Java bean as if it were a COM or ActiveX component. The details of this sort of interoperability are beyond the scope of this chapter, however.

Beans can be used at three levels, by three different categories of programmers:

  • If you are writing applications that use beans developed by other programmers (perhaps using a beanbox visual development tool to combine those beans into an application), you don't actually need to be familiar with the JavaBeans API. You only need to know how to use your beanbox and be familiar with the documentation for specific beans that you use.

  • If you are writing actual beans, you need to be familiar with JavaBeans naming conventions, so that you can follow those conventions when designing your bean's API. You may also use the java.beans API to create auxiliary classes that integrate it more tightly into beanboxes.

  • If you are developing GUI editors, application builders, or other "beanbox" tools, you need the JavaBeans API to manipulate beans within these tools. Note that the ShowBean program of Example 11-30 was a simple kind of beanbox.

This chapter explains how to use the JavaBeans API at the second and third levels: how to write beans and their auxiliary classes, and how to write programs that manipulate beans. The emphasis is on writing beans, but Example 15-10 shows the Bean class, which implements the inner workings for the ShowBean beanbox of Chapter 11. Contents of this chapter include:

  • Basic bean concepts and terminology

  • Requirements for the simplest beans

  • Packaging beans in JAR files

  • Providing additional information about beans with the BeanInfo class

  • Defining property editors to allow custom editing of bean properties

  • Defining bean customizers to allow customization of an entire bean

  • Instantiating beans, introspecting on them, using property editors to convert property values to and from strings, and using Java reflection to set named properties and invoke named methods

    [ Team LiB ] Previous Section Next Section