JavaBeans is a software component architecture that extends the power of the Java language by enabling well-formed objects to be manipulated visually at design time in a pure Java builder tool, such as NetBeans, JBuilder, or Eclipse. Such well- formed objects are referred to as JavaBeans or simply beans . The classes that define the beans, referred to as JavaBeans components or bean components , or simply components , conform to the JavaBeans component model with the following requirements:
A bean must be a public class.
A bean must have a public no-arg constructor, though it can have other constructors if needed. For example, a bean named MyBean must either have a constructor with the signature
public MyBean();
or have no constructor if its superclass has a no-arg constructor.
A bean must implement the java.io.Serializable interface to ensure a persistent state.
A bean usually has properties with correctly constructed public accessor (get) methods and mutator (set) methods that enable the properties to be seen and updated visually by a builder tool.
A bean may have events with correctly constructed public registration and deregistration methods that enable it to add and remove listeners. If the bean plays a role as the source of events, it must provide registration methods for registering listeners. For example, you can register a listener for ActionEvent using the addActionListener method of a JButton bean.
The first three requirements must be observed , and therefore are referred to as minimum JavaBeans component requirements . The last two requirements are dependent on implementations . It is possible to write a bean without get/set methods and event registration/deregistration methods.
A JavaBeans component is a special kind of Java class. The relationship between JavaBeans components and Java classes is illustrated in Figure 27.1.
Every GUI class is a JavaBeans component, because
it is a public class,
it has a public no-arg constructor, and
it is an extension of java.awt.Component , which implements java.io.Serializable .