Advanced Java Programming
Advanced Java Programming
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:
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.
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.
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;
}
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.
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.
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.