0% found this document useful (0 votes)
141 views7 pages

Advanced Java Programming

This document provides an overview of Java beans, including: 1. An introduction to Java beans, their advantages, and the Bean Development Kit (BDK). 2. Details on introspection and how it allows external applications to query bean properties, methods, and events. 3. Descriptions of bound and constrained properties, the BeanInfo interface, persistence, and customizers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
141 views7 pages

Advanced Java Programming

This document provides an overview of Java beans, including: 1. An introduction to Java beans, their advantages, and the Bean Development Kit (BDK). 2. Details on introspection and how it allows external applications to query bean properties, methods, and events. 3. Descriptions of bound and constrained properties, the BeanInfo interface, persistence, and customizers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 7

Unit 2

1.Introduction to Java Beans


2.Advantages of Java Beans
3.BDK
4.Introspection
5.Using Bound & Constrained Properties
6.Bean Info Interface
7.Persistence
8.Customizes
9.Java Beans API

1. Introduction to Java Beans:


--A Java Bean is a 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 a simple function,
such as obtaining an inventory value, or a 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 also be invisible to a user. Software to decode a stream of multimedia
information in real time is an example of this type of building block.
--Finally, 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. Software to generate a
pie chart from a set of data points is an example of a Bean that can execute locally.
However, a Bean that provides real-time price information from a stock or commodities
exchange would need to work in cooperation with other distributed software to obtain its
data.

2.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 another application
can be controlled.
• Auxiliary software can be provided to help 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.

3.BDK (Beans Development Kit) :


--Is a development environment to create, configure, and test JavaBeans.
--The features of BDK environment are:
-Provides a GUI to create, configure, and test JavaBeans.
-Enables you to modify JavaBean properties and link multiple JavaBeans in an
application using BDK.
-Provides a set of sample JavaBeans.
-Enables you to associate pre-defined events with sample JavaBeans

The components of BDK development environment are:


• ToolBox
• BeanBox
• Properties
• Method Tracer
ToolBox window: Lists the sample JavaBeans of BDK.The following figure shows the
ToolBox window

BeanBox window: Is a workspace for creating the layout of JavaBean application. The
following figure shows the BeanBox window:

Properties window: Displays all the exposed properties of a JavaBean. You can modify
JavaBean properties in the properties window.The following figure shows the Properties
window:
Method Tracer window: Displays the debugging messages and method calls for a
JavaBean application. The following figure shows the Method Tracer window:

4.Introspection:

“Introspection: Is the ability of a JavaBean to allow an external application to query


the properties, methods, and events supported by it. “

At the core of Java Beans is introspection. This is the process of analyzing a Bean to
determine its capabilities. This is an essential feature of the Java Beans API because it
allows another application, such as a design tool, to obtain information about a
component. Without introspection, the Java Beans technology could not operate.

There are two ways in which the developer of a Bean can indicate which of its properties,
events, and methods should be exposed. With the first method, simple naming
conventions are used. These allow the introspection mechanisms to infer information
about a Bean. In the second way, an additional class that extends the BeanInfo interface
is provided that explicitly supplies this information. Using Naming conventions approach
is examined here.

Using Naming Conventions:

A property is a subset of a Bean’s state. The values assigned to the properties determine
the behavior and appearance of that component. A property is set through a setter method.
A property is obtained by a getter method. There are two types of properties: simple and
indexed.

Design Patterns for 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(T arg)

A read/write property has both of these methods to access its values. A read-only property
has only a get method. A write-only property has only a set method. Here are three
read/write simple properties along with their getter and setter methods:

Example:
private double depth, height, width;
public double getDepth( ) {
return depth;
}
public void setDepth(double d) {
depth = d;
}
public double getHeight( ) {
return height;
}
public void setHeight(double h) {
height = h;
}
public double getWidth( ) {
return width;
}
public void setWidth(double w) {
width = w;
}

Design properties for 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 void setN(int index, T value);
public T[ ] getN( );
public void setN(T values[ ]);

Design Patterns for Event:

Beans use the delegation event model that was discussed earlier in this book. Beans can
generate events and send them to other objects. These can be identified by the following
design patterns, where T is the type of the event:
public void addTListener(TListener eventListener)
throws java.util.TooManyListenersException
public void removeTListener(TListener eventListener)
These methods are used to add or remove a listener for the specified event.

--The version of AddTListener( ) that does not throw an exception can be used to
multicast an event, which means that more than one listenercan register for the event
notification.
--The version that throws TooManyListenersException unicasts the event, which means
that the number of listeners is restricted to one.
--In either case, removeTListener( ) is used to remove the listener.

5.Bound and Constrained 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.
A class that handles this event must implement the PropertyChangeListener interface.
A Bean that has a constrained property generates an event when an attempt is made to
change its value. It also generates an event of type PropertyChangeEvent. It too is sent
to objects that previously registered an interest in receiving such notifications. However,
those other objects have the ability to veto the proposed change by throwing a
PropertyVetoException. This capability allows a Bean to operate differently according
to its run-time environment. A class that handles this event must implement the
VetoableChangeListener interface.

6.Using the BeanInfo Interface:

The BeanInfo interface enables you to explicitly control what information is available.
The BeanInfo interface defines several methods, including these:

PropertyDescriptor[ ] getPropertyDescriptors( )
EventSetDescriptor[ ] getEventSetDescriptors( )
MethodDescriptor[ ] getMethodDescriptors( )

7.Persistence:

Persistence is the ability to save the current state of a Bean, including the values of a
Bean’s properties and instance variables, to nonvolatile storage and to retrieve them at a
later time. The object serialization capabilities provided by the Java class libraries are
used to provide persistence for Beans. The easiest way to serialize a Bean is to have it
implement the java.io.Serializable interface, which is simply a marker interface.
Implementing java.io.Serializable makes serialization automatic. Your Bean need take
no other action. Automatic serialization can also be inherited. Therefore, if any superclass
of a Bean implements java.io.Serializable, then automatic serialization is obtained.

8.Customizers:
A Bean developer can provide a customizer that helps another developer configure the
Bean. A customizer can provide a step-by-step guide through the process that must be
followed to use the component in a specific context. Online documentation can also be
provided. A Bean developer has great flexibility to develop a customizer that can
differentiate his or her product in the marketplace.

The JavaBeans model provides an alternative to property sheets. It specifies an interface


called Customizer to enable bean authors to implement a bean-specific customizer. Such
a customizer can be a simple panel with related properties grouped together or a
sophisticated wizard that allows the user to navigate through a hierarchy of screens. Since
builder tools recognize any customizer that implements the Customizer interface, it can
be launched from any builder tool that has the bean installed.

Customizer Interface:
The Customizer interface is simple and has only three methods:
– addPropertyChangeListener(PropertyChangeListener listener):This method adds a
propertyChange event listener. By registering as a listener, the target object(s) (typically
the builder tool and property sheet at design time or bean or bean context at runtime) can
receive notification and the modified value of the property. When the target object
receives a propertyChange event, it retrieves the setter Method object and the property
value from the PropertyChangeEvent object. Using this Method object and the property
value, the target object then invokes the setter method in the bean to modify the property.
– removePropertyChangeListener (PropertyChangeListener listener):This removes a
property change event listener.
– setObject(Object bean):The builder tool calls this method to pass the target bean
instance to the customizer. It is called only once, which is before the builder tool launches
the customizer.
9.The Java Beans API:
The Java Beans functionality is provided by a set of classes and interfaces in the
java.beans package.

You might also like