Introduction to Java Beans
A Java Beans is software
component that has been designed to be reusable in a variety of different
environments. There is no restriction on the capability of a Bean. It may
perform simple function, such as checking the spelling of a document, or
complex function, such as forecasting the performance of a stock portfolio. A
bean may be visible to an end user. One example of this is a button on a
graphical user interface. A bean may be designed to work autonomously on a
user’s workstation or to work in cooperation with a set of other distributed
components.
Advantages of Java Beans
· A bean obtains all the
benefits of Java’s “write once, run-anywhere” paradigm.
· The properties, events and
methods of a bean that are exposed to an application builder tool can be
controlled.
· A bean may be designed to
operate correctly in different locales, which makes it useful in global
markets.
· Auxiliary software can be
provided to help a person configure a bean.
· The configuration settings
of a bean can be saved in persistent storage and restored at a later time.
· A bean may register to
receive events from other objects and can generate events that are sent to
other objects.
BDK Introspection
Introspection is the
process of analyzing a bean to determine its capabilities. This is a very
important feature of Java Bean API, because it allows an application builder
tool to present information about a component to a software designer. Without
introspection, the java beans technology could not operate. One way exposed the
properties, events and methods of bean to application builder tool is using
simple naming conventions.
Design pattern for
properties
Property is a subset of a
bean’s state. The values that are assigned to the properties determine the
behavior and appearance of that component.
Simple properties:
A
simple property has a single value. It can be identified by the following
design
patterns, where N is the name of the property and T is its type.
Public T getN(
);
Public void setN( );
Boolean properties:
A Boolean property has a
value of true or false. It can be identified by the following design patterns,
where N is name of the property.
Public Boolean isN ( );
Public Boolean getN( );
Public void setN(Boolean
value);
Indexed properties:
An indexed property
consists of multiple values. It can be identified by the following design
patterns, where N is the name of the property and T is its type
Public
T getN(int index);
Public
T[ ] getN( );
Public
void setN(T values[ ]);
Using Bound Properties
A bean that has a bound
property generates an event when the property is changed. The event is of type
PropertyChangeEvent and is sent to objects that previously registered an
interest in receiving such notifications.
Example 17: Application
that uses TickTock bean to automatically control the Color bean
Steps:
1. Go to menu bar of the bean
box and select Edit | Events | propetyChange. We can now see a line extending
from the button to the cursor
2. Move the cursor so that it
is inside the Colors bean display area , and click the left mouse button. See
the Event Target Dialog dialog box.
3. the dialog box allows your
to choose a method that should be invoked when this event occurs. Select
the entry labeled “change” and click the Ok button.
Using BeanInfo Interface
This interface defines
several methods, including these:
PropertyDescription[
] getPropertyDescriptors( )
EventSetDescriptor[
] getEventSetDescriptors( )
MethodDescriptor[
] getMethodDescriptors( )
The above methods will
return array of objects that provide information about the properties, events,
and methods of bean. SimpleBeanInfo is a class that provides default
implementations of the BeanInfo interface, including the three methods just
shown. We can extend this class and override on or more of them.
Constrained Properties
A bean that has a constrained
property generates an event when an attempt is made to change its value. The
event is of type PropertyChangeEvent. It is sent to objects that previously
registered an interest in receiving such notifications. This capability allows
a Bean to operate differently according to its run-time environment. A
Persistence
Persistence is the ability
to save a Bean to nonvolatile storage and retrieve it at a later time. The
information that is particularly important are the configuration settings.
Interface
|
Description
|
AppletInitializer
|
Methods present in this interface are used to initialize Beans that
are also applets
|
BeanInfowww.jntuworld.com
|
This interface allows a designer to specify information about the
properties, events and methods of a Bean.
|
Customizer
|
This interface allows a designer to provide a graphical user interface
through which a Bean may be configured.
|
DesignMode
|
Methods in this interface determine if a Bean is executing in design
mode.
|
PropertyChangeListener
|
A method in this interface is invoked when a bound property is
changed.
|
Visibility
|
Methods in this interface allow a bean to execute in environments
where graphical user interface is not available.
|
Class
|
Description
|
BeanDescriptor
|
This class provides information about a Bean.
|
Beans
|
This class is used to obtain information about a Bean
|
IntrospectionException
|
An exception of this type is generated if a problem occurs when
analyzing a bean.
|
PropertyChangeEvent
|
This event is generated when bound or constrained properties are
changed.
|
PropertyDescriptor
|
Instances of this class describe a property of a Bean
|
Comments
Post a Comment
Thanks for your comments!