Chapter 4-Java Beans
Chapter 4-Java Beans
Page 1
Chapter 4
JavaBeans
Introduction
JavaBeans are reusable software components for Java that can be manipulated visually in
a builder tool. JavaBeans components are known as beans. Particularly, they are classes
written in the Java programming language conforming to a particular convention. Using
visual application builder tools, software components can be composed into applets,
applications, servlets, and composite components.
Beans are dynamic in that they can be changed or customized. Through the design
mode of a builder tool you can use the Properties window of the bean to customize the
bean and then save (persist) your beans using visual manipulation. You can select a bean
from the toolbox, drop it into a form, modify its appearance and behavior, define its
interaction with other beans, and combine it and other beans into applet, application,
servlet, or a new bean.
Nawaraj Paudel
Page 2
patterns for their method signatures and class definitions, tools that recognize these
patterns can "look inside" a Bean and determine its properties and behavior.
Introspection allows a Bean's state to be manipulated at design time that is, at the
time it is being assembled as a part within a larger program. For this to work, method
signatures within Beans must follow a certain pattern so that introspection tools recognize
how Beans can be manipulated, both at design time and at run time.
In effect, Beans publish their attributes and behaviors through special method
signature patterns that are recognized by beans-aware application construction tools.
However, these construction tools are not required to build or test your beans. The pattern
signatures are easily recognized by human readers, as well as builder tools. One of the
first things you'll learn when building beans is how to recognize and construct methods
that adhere to these patterns.
Nawaraj Paudel
Page 3
While Beans are intended to be used primarily with builder tools, they need not be. Beans
can be manually manipulated by text tools through programmatic interfaces.
The ToolBox window displays the JavaBeans that are currently installed in the BeanBox,
such as the Beans they come with the BeanBox demo. When the BeanBox starts, it
automatically loads its ToolBox with the Beans in the JAR files contained in the bean/jars
directory. You can add additional Beans, such as your own Beans, to the ToolBox.
The BeanBox window itself appears initially as an empty window. You use this
empty window, sometimes referred to as a "form" by other builder tools, for building
applications.
The Properties window displays the current properties for the selected Bean. If no
Bean is selected, such as when you first start the BeanBox or if you click in the BeanBox
window's background, then the Properties window displays the BeanBox properties. You
can use the Properties window or sheet to edit a Bean's properties.
Nawaraj Paudel
Page 4
Nawaraj Paudel
Page 5
To complete the example program, add a stop button. Repeat the steps you took when
you added the start button and connected it to the appropriate Juggler action.
1. Select OurButton from the list of available Beans in the Toolbox menu, and then
drop it below the start button in the BeanBox.
When the new button is the currently selected Bean, the appropriate property editor
appears.
2. Edit the label field of the property sheet to read "stop."
3. Hook up the action event from the stop button to the Juggler's stopJuggling eventhandler method. Make sure the stop button is the currently selected bean.
4. Select the actionPerformed event from the Edit/Events menu.
5. Drag the event line from the stop button source to the Juggler Bean target. Press
and release the mouse button with the connection line over the Juggler.
6. The dialog of applicable event-handler methods defined for the Juggler Bean
displays; select the stopJuggling event and click OK.
You should now be able to start and stop the juggler by pressing the appropriate button.
Nawaraj Paudel
Page 6
User must create the manifest file to provide which components in the JAR file are java
beans. You can put any number of files in the manifest file entry but if you have class
files acting as java beans then each entry of the file must be immediately followed by the
line Java-Bean: True
For example,
Name: SimpleBean.class
Java-Bean: True
Name the file as SimpleBean.mft (or .tmp or of your choice)
Here if you have other resources you can append them using Name: resource path. While
including class name if you have created package write full path including package. The
above file indicates that there is only one class file in JAR file.
Generate a JAR file
jar cfm SimpleBean.jar SimpleBean.mft SimpleBean.class
Other three steps are as done previously.
Introspection
The introspection services provided by the JavaBeans API are divided into two parts:
low-level services and high-level services. The low-level API services are typically used
by application builder tools, which make heavy use of bean internals to provide advanced
development features. This level of access isn't appropriate for application developers,
however, because it exposes bean internals that aren't meant to be accessible at the
application level. For developers, the high-level API services are more appropriate. The
high-level services provide access to a limited portion of a bean's internals, which
typically consist of a bean's public properties, methods, and events. The high-level
introspection services rely heavily on the low-level services; the primary difference
between the two is that the high-level services limit access to bean internals.
There are two approaches to introspect. The automatic approach to introspection
provides bean information automatically. For those cases in which bean developers want
to get up close and personal with bean information, there is an explicit approach.
The first approach of JavaBeans automatically assessing information about a bean
uses the reflection facilities in the core Java API. The reflection facilities are responsible
for analyzing the low-level structure of a bean and returning information. The
introspection facilities in the JavaBeans API take this information and apply design
patterns to it to determine the specifics about the public properties, methods, and events
supported by a bean.
The explicit approach to defining bean information requires a little more work on the
part of bean developers. The JavaBeans API provides support classes that are used by
developers to explicitly define the public information for a bean. Keep in mind that this
approach is purely optional and can even be used in combination with the automatic
approach. In other words, developers are free to explicitly provide some bean information
and allow the introspection facilities to automatically determine the rest. It's important to
note that automatic introspection is the default approach taken by JavaBeans, and is
superseded only if explicit information is available. This ability to leverage both
introspection approaches provides lots of flexibility for bean developers.
Nawaraj Paudel
Page 7
Design Patterns
Bean developers are not required to add any additional support for introspection in their
code. They only have to structure the code for the bean itself because JavaBeans relies on
the naming and type signatures of the methods defined in the code for a bean to
determine what properties, methods, and events the bean supports for public use. In other
words, the conventions used in the code for a bean are also used as a means to introspect
the bean.
The automatic approach to JavaBeans introspection relies heavily on the naming
conventions and type signatures of a bean's methods, including methods for registering
event listeners. The approach relies on design patterns, which are standardized naming
conventions and type signatures that are used to define bean methods based on their
function.
There are a variety of different design patterns for specifying everything from simple
properties to event sources. All of these design patterns rely on some type of consistent
naming convention for methods and their related arguments and types. Keep in mind that
this approach to introspection is not only convenient from the perspective of JavaBeans,
but it also has the intended side effect of encouraging bean developers to use a consistent
set of naming conventions. Here are the three major types of design patterns, each of
which are discussed.
Property design patterns
Event design patterns
Method design patterns
1. Simple Property
Simple properties consist of all single-valued properties, which include all built-in
Java data types as well as classes and interfaces. For example, int, long, float,
Color, Font, and boolean are all considered simple properties. The design
patterns for simple property accessor (getters and setters) methods follow:
public <PropertyType> get<PropertyName>()
Nawaraj Paudel
Page 8
implements
2. Boolean Property
Boolean Properties are technically simple properties of boolean type. They
have an optional design pattern for the getter method that can be used to make it
more clear that a property is of boolean type. This design pattern follows:
public boolean is<PropertyName>() //preferred one
public boolean get<PropertyName>()
The only difference between this design pattern and the one for simple properties
is that it uses the word is instead of get in the name. It is fairly common for
Java developers to access boolean properties with an is method, which is why
JavaBeans supports the use of this special case design pattern. The following is an
Nawaraj Paudel
Page 9
3. Indexed Property
An indexed property is an array of simple properties. Because indexed properties
require slightly different accessor methods, it only makes sense that their design
patterns differ a little from other properties. Here are the design patterns for
indexed properties:
//Methods to access individual values
public <PropertyType> get<PropertyName>(int index);
public void set<PropertyName>(int index, <PropertyType>
value);
//Methods to access the entire indexed property array
public <PropertyType>[] get<PropertyName>();
public void set<PropertyName>(<PropertyType>[] value);
The first pair of design patterns defines accessor methods for getting and setting
individual elements in an indexed property. The second pair of patterns defines
accessor methods for getting and setting the entire property array as a whole. For
example,
public
public
public
public
Nawaraj Paudel
Page 10
In these design patterns, <EventListenerType> refers to the object type of the event
listener being registered with the bean. JavaBeans requires that this
object implement the EventListener interface and have its name end with
Listener. Therefore, if you want to add support for an event listener implementing the
ActionListener interface, you would do so like this:
public void addActionListener(ActionListener al);
public void removeActionListener(ActionListener al);
The ActionListener interface is used to receive action events such as mouse clicks. In
this example, action listeners can easily be registered to receive actions by calling the
addActionListener() method, or removed as a listener by calling the
removeActionListener() method.
The event registration design patterns discussed thus far are designed for beans that
support multicast events, meaning that there can be multiple registered event listeners.
JavaBeans also supports the use of unicast event sources, which are beans that enable
only one listener at a time to receive event notifications. In other words, unicast beans
enable only one event listener to be registered at a particular time. If an attempt is made
to register more than one listener with a unicast bean, the add<EventListenerType>()
method is thrown a TooManyListenersException exception. The design pattern for this
method follows:
public
void
add<EventListenerType>(<EventListenerType>
TooManyListeners;
Here is the same action listener example designed for a unicast event source:
x)throws
Nawaraj Paudel
public
void
Page 11
addActionListener(ActionListener
al)
throws
TooManyListeners;
public void removeActionListener(ActionListener al);
Bound Properties
Sometimes when a Bean property changes, another object may want to be notified of the
change, and react to the change. Whenever a bound property changes, notification of the
change is sent to interested listeners. A Bean containing a bound property must maintain a
list of property change listeners, and alert those listeners when the bound property
changes. The convenience class PropertyChangeSupport implements methods that add
and remove PropertyChangeListener objects from a list, and fires
PropertyChangeEvent objects at those listeners when the bound property changes. Your
Beans can inherit from this class, or use it as an inner class. An object that wants to listen
for property changes must be able to add and remove itself from the listener list on the
Bean containing the bound property, and respond to the event notification method that
signals a property change. By implementing the PropertyChangeListener interface the
listener can be added to the list maintained by the bound property Bean, and because it
implements the PropertyChangeListener.propertyChange method, the listener can
respond to property change notifications.
The PropertyChangeEvent class encapsulates property change information, and is
sent from the property change event source to each object in the property change listener
list via the propertyChange method.