Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

JavaBeans 101, Part II

 
Online Training Index

101, Part II:

Introduction | Page 2 | Page 3 | Page 4



Part II of the JavaBean tutorial teaches you how to write the code for a simple JavaBean. Later, you will use the BeanBox tool to run, test, and change your Bean. You can also create JavaBeans and modify existing Beans using GUI building tools. For a review of JavaBeans Part I, see JavaBeansTM Part I.

A Look at JavaBeans

A JavaBean is a Java class that defines properties and that communicates with other Beans via events. Properties can be defined within the JavaBean class definition, or they can be inherited from other classes. A Bean, however, is not required to inherit from any particular class or interface.

JavaBeans that represent graphical components and that are meant to be visible must inherit from a java.awt.Component, such as the java.awt.Canvas class, so that they can be added to visual containers. Typically, a JavaBean represents visible graphical objects and thus it extends an AWT component. In fact, if you start with a class that defines an AWT component, it is easy to modify that class to make it a JavaBean.

Note that there are Beans that are not meant to be visible, and they are referred to as invisible Beans. They are identical to other JavaBeans except that they have no GUI representation. They are able to call methods, fire events, save persistent state, and so forth; they just have no screen appearance of their own. Invisible Beans may still be represented visually in a builder tool, and they may be customized within that tool.

A Look at Properties

Properties are analogous to instance variables inside of objects. Event handlers are analogous to behaviors or methods defined for objects. The main difference is that properties and events are exposed to builder tools to enable visual manipulation of beans. Values assigned to properties can be modified at design time. Similarly you can visually select event handlers from list boxes to link the events generated by one Bean to corresponding event handlers available in another Bean.

When you define properties in a Bean, you typically declare them as private types. Then, so that other Beans and applications can access these properties, you define public get and set methods for each property. A JavaBean must also extend the java.io.Serializable interface so that its state can be made persistent.

Putting It All Together

Once you've written the Java class to define a Bean, compile it to verify that you've made no obvious errors in the code. Then, test it using the BeanBox. These steps are explained in detail in the following sections.

  • Creating a Bean

    This section describes the basic steps to create a simple JavaBean. You write a Bean with a simple property, then compile it. You also go through the steps to create a JAR file for the Bean so that you can test it with the BeanBox.

  • JavaBeans and Packages

    Packages are a useful way to keep related Beans and supporting Bean class files together. This lesson teaches you how to use packages with your JavaBeans.

  • Adding Labels to Beans

    This lesson gives you additional hands-on experience with properties and JavaBeans.

  • Adding Properties

    This lesson focuses on the more complex types of properties that you can add to Beans. It shows you how to implement bound and constrained properties.

  • Manipulating Properties

    This part of the tutorial shows you how to programmatically override default property values in your JavaBeans.

  • Handling Events

    JavaBeans use events to communicate to other Beans, so it is important to understand how to handle events. This section covers all you need to know to handle events with JavaBeans, including registering event listeners, dispatching events, and using events to control Bean behavior.

Introduction | Page 2 | Page 3 | Page 4

Coffecup Logo

Beth Stearns is the principal partner of ComputerEase Publishing, a computer consulting firm she founded in 1982. Her client list includes Sun Microsystems, Inc., Silicon Graphics, Inc., Oracle Corporation, and Xerox Corporation. Among her publications are the "Java Native Interface" chapter in "The Java Tutorial Continued" book in the Addison Wesley Java series, "The EJB Programming Guide" for Inprise Corporation, and "Understanding EDT", a guide to Digital Equipment Corporation's text editor. Most recently, she is co-author with Vlada Matena of the forthcoming Addison Wesley Java series book, "Applying Enterprise JavaBeans: Component-Based Development for the J2EE Platform."


Have a question about programming? Use Java Online Support.