Bean Basics

We begin our discussion of beans with some basic concepts and terminology. Any object that conforms to certain basic rules and naming conventions can be a bean; there is no Bean class that all beans are required to subclass. Many beans are Swing or AWT components, but it is also quite possible, and often useful, to write “invisible” beans that don’t have an onscreen appearance. ( Just because a bean doesn’t have an onscreen appearance in a finished application doesn’t mean that it can’t be visually manipulated by a beanbox tool, however.)

A bean exports properties, events, and methods. A property is a piece of the bean’s internal state that can be programmatically set and queried, usually through a standard pair of get and set accessor methods. A bean may generate events in the same way that a GUI component, such as a JButton, generates ActionEvent events. The JavaBeans API uses the same event model (in fact, it defines the event model) used by Swing and AWT GUIs in Java 1.1 and later. See Chapter 11 for a full discussion of this model. A bean defines an event by providing methods for adding and removing event listener objects from a list of interested listeners for that event. Finally, the methods exported by a bean are simply any public methods defined by the bean, excluding those methods used to get and set property values and register and remove event listeners.

In addition to the regular sort of properties just described, the JavaBeans API also provides ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.