JavaBeans technology is a significant extension and enhancement of the Java language that enables programmers to rapidly build applications by assembling objects and testing them during design time. This technology brings an era of component computing as new applications can be created by connecting various off-the-shelf components using tools such as JBuilder.
JavaBeans are reusable software components or software programs that can be developed and assembled easily to create sophisticated applications.
JavaBeans are portable, platform-independent, reusable software components written in Java. This enables developers to write reusable components once and use them anywhere, thereby benefiting from the platform-independent power of Java. Reusable software components can be simple, e.g. buttons, text fields, list boxes, scroll bars and dialogs. These are traditionally sold by third parties. More complex software components such as calendars and spreadsheets are now being developed and sold.
JavaBeans implement the Java Component Model, which gives them a variety of benefits, exposing their features, representing features for use and making JavaBeans especially reusable. This component model is defined in the JavaBeans Specification – an official document released by Sun Microsystems. It defines standards for exposing features and for using the exposed features of a JavaBean. These together make a JavaBean a reusable software component. The exposure of features is based on design patterns.
Beans contain the following features:
· Introspection – enables a builder tool to analyze how a bean works
· Customization – enables a builder to use an application builder tool to customize the appearance and behavior of a Bean
· Events – enable Beans to communicate and connect together
· Properties – enable developers to customize and program with Beans
· Persistence – enables developers to customize Beans in an application builder tool, then retrieve those Beans with customized features intact for future use
JavaBeans are written just as any other Java class. Existing components, applets, or Java classes can be turned into JavaBeans. All AWT and Swing user interface classes are actually JavaBeans.
Why Use JavaBeans
If code is highly reusable or if a generic component that can be used in several applications or many times within a single application is required, JavaBeans make application development faster, easier and possibly less costly.
Some important reasons for using JavaBeans are:
1. Creating customizable, reusable components that enable you to cut down on development time.
For example, consider a simple button. Creating a JavaBean provides you with a prototype for buttons in your application. You can customize the Bean to meet the requirements for any type of button you need in your application. Rather that coding several buttons with different sizes or colors, you simply use your JavaBean, customizing it as needed.
2. Use existing Beans available on the Internet and through other sources to speed your development process.
Since many functionalities are already been implemented by JavaBeans, you can speed up your development process by simply obtaining the beans you are interested in, customizing them to meet your specific needs and then using them in your applications. There are many sources on the Internet for downloading JavaBeans, here is one:
This page has a searchable list of Beans from third-party vendors for application areas such as authoring tools, charts, graphs, databases, GUI, etc.
JavaBeans offer benefits that include:
· Portability – JavaBeans are written in Java, a portable language so they too are portable
· Uniformity – the JavaBean Specification defines standards for exposing features and for using the exposed
features. This also enhances interoperability.
· Simplicity – JavaBeans use simple standards to expose features and use exposed features
· Distributability – all Java objects are distributable
Beans and Objects
JavaBeans enable well-formed objects, referred to as Java beans or simply beans, to be manipulated in a building tool like JBuilder during design time. The classes that define the beans are called JavaBeans components or bean components or simply components. These classes must conform to the JavaBeans component model with the following requirements:
1. A bean component must be a public class
2. A bean component must have a public default no argument constructor (can have other constructors if needed or no constructor if its super class has a default constructor). For example, a bean named MyBean will have a constructor with the following signature
3. A bean component must implement the Serializable interface to ensure a persistent state, which maybe required when JavaBeans are used in other tools
Implementation Dependent Requirements
4. A bean component usually has properties with public accessor methods that enable components to be seen and updated visually by a builder tool. The accessor methods must conform to the naming patterns or be explicitly specified for the properties to be manipulated. Naming patterns for accessor methods are get<PropertyName>() for getting the property value and set<PropertyName>() for setting the property value.
5. A bean component may have events with public registration 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.
The first three requirements must be observed by all beans. However, it is possible to write a bean without accessor or event registration methods, so the last two are not always required.
From the requirements above, a JavaBeans component can be redefined as a serializable public class with a default constructor.
Example: Writing a Simple Javabeans Component: MessagePanel bean
Since Java.awt.Component implements Serializable, and MessagePanel is a subclass of Component, MessagePanel is serializable.
The MessagePanel class is a public class with a default constructor MessagePanel() - line 13. MessagePanel is a serializable public class with a default constructor, it is therefore a JavaBeans component.
The MessagePanel class has the properties or instance variables message, xCoordinate, yCoordinate and centered.
The getMessage(), getXCoordinate(), getYCoordinate(), and isCentered() methods read the message, xCoordinate, yCoordinate and centered properties respectively; and the setMessage(), setXCoordinate(),setYCoordinate(), and isCentered() methods update these properties.
The message is painted on the panel starting at the position specified by coordinates xCoordinate and yCoordinate.
The getPreferredSize() lines 59-61, and getMinimumSize() lines 63-65, methods are overridden to specify the preferredSize and minimumSize properties for instances of MessagePanel.
The MessagePanel class also has two other methods, moveLeft() lines 67-73 and moveRight() lines 75-81, for moving the message to the left and to the right on the panel
Note: In this example, code has been written to declare properties and accessor methods. However, JBuilder’s BeansExpress can be used to automatically generate code.
JavaBeans and Java Builder Tools
Since a JavaBeans component is a Java class, it can be used wherever a class is applicable. A JavaBeans component can be created, its properties can be accessed and its methods can be invoked in a scripting environment, just like any regular class. But, JavaBeans components are specially designed for use in builder tools that enable the bean user to visually manipulate the beans during design time. Examples of builder tools are JBuilder, Visual Café, MS Visual Basic, FrontPage, even MS Word.
A builder tool can discover contents (properties, methods and events) and expose them to the bean user for visual manipulation during design time. The component inspector is the user interface on which the contents are presented and on which the user can update property values and register events of the bean.
The builder tool also has an internal component, the reflector, which is responsible for communication between the bean and the component inspector during design time. The reflector analyzes the bean and presents its properties and events on the component inspector. When the user modifies the properties, the reflector applies the changes to the bean by invoking the set method of the properties.
During design time, the user interacts with the inspector, and the reflector controls the execution of the beans. During runtime, the bean runs just like a regular object.
If the naming patterns for properties and events are followed, the reflector automatically recognizes and presents it to the inspector. If not, a bean information class that implements the BeanInfo interface must be provided to describe the properties and events of the bean to the reflector in order for the inspector to reveal them to the user.
Using JavaBeans in JBuilder
The major application of JavaBeans is rapid applications development (RAD), a powerful feature used in software development tools for visual programming. RAD’s click-and-drop capability enables programs to be written with little or no code, thereby improving software reusability and productivity
In JBuilder, RAD is completely based on JavaBeans and each RAD component is a bean accessible from the component palette in the UI designer. This means JavaBeans have to be added to the component palette first before they can be used.
Example Adding a Bean to the Component Palette: the MessagePanel bean
Developing Components Vs Using Components
Note that there is a difference between developing components and using components.
From the user’s perspective, a component has properties, methods and events. The user may use the component to create beans in a builder tool or in scripting programming. The user creates the bean and customizes its behavior by changing properties and writing code to carry out responses to events.
From the writer’s perspective, a component is designed for use in a visual environment, where its properties and events are exposed during design time. To have the builder tool expose the properties and events, some design patterns must be followed.
Three major differences between developing and using components:
1. To the user, a component is a black box. The user manipulates it through public accessor methods and events without knowing how the component is implemented. The component writer knows all the details if the component is developed from scratch.
2. Components are designed for use by many different customers so they need to be useful in a wide range of applications. They should be customizable through properties, methods and events.
3. Components are designed for reuse. Users can incorporate components in many different combinations, orders and environments so the design of a component should not impose any restrictions on what the user can do with it or when the user can use it. The properties should be designed in such a way that the user can set them in whatever way they want. Events and methods should be designed to function independently of their order of occurrence at run time.
Component Development Process
As in the development of any software, developing JavaBeans components requires an engineering approach that involves the following steps.
Components can be efficiently created using JBuilder’s BeansExpress, a visual bean designer that helps create the JavaBeans component without writing extensive code.