Professional Documents
Culture Documents
1. Position the cursor on the ToolBox entry labeled Molecule and click the left mouse
button. You should see the cursor change to a cross.
2. Move the cursor to the BeanBox display area and click the left mouse button in
approximately the area where you wish the Bean to be displayed. You should see a
rectangular region appear that contains a 3-D display of a molecule. This area is
surrounded by a hatched border, indicating that it is currently selected.
3. You can reposition the Molecule Bean by positioning the cursor over one of the
hatched borders and dragging the Bean.
4. You can change the molecule that is displayed by changing the selection in the
Properties window. Notice that the Bean display changes immediately when you
change the selected molecule.
5. Go to the menu bar of the BeanBox and select Edit | Events | action |
actionPerformed. You should now see a line extending from the button to the cursor.
Notice that one end of the line moves as the cursor moves. However, the other end
of
the line remains fixed at the button
6. Move the cursor so that it is inside the Molecule Bean display area, and click the
left mouse button. You should see the Event Target Dialog dialog box.
7.The dialog box allows you to choose a method that should be invoked when this
button is clicked. Select the entry labeled "rotateOnX" and click the OK button. You
should see a message box appear very briefly, stating that the tool is "Generating nd
compiling adaptor class.“
Test the application. Each time you press the button, the molecule should move a
few degrees around one of its axes.
Now create another instance of the OurButton Bean. Label it "Rotate Y" and map its
action event to the "rotateY" method of the Molecule Bean. The steps to do this are
very similar to those just described for the button labeled "Rotate X".
Test the application by clicking these buttons and observing how the molecule
moves.
Before developing your own Bean, it is necessary for
you to understand JAR (Java Archive) files, because
tools such as the BDK expect Beans to be packaged
within JAR files.
A JAR file allows you to efficiently deploy a set of
classes and their associated resources.
For example, a developer may build a multimedia
application that uses various sound and image files.
A set of Beans can control how and when this
information is presented. All of these pieces can be
placed into one JAR file.
Also, the elements in a JAR file are compressed,
which makes downloading a JAR file much faster than
separately downloading several uncompressed files.
A developer must provide a manifest file to indicate which
of the components in a JAR file are Java Beans.
An example of a manifest file is provided in the following
listing. It defines a JAR file that contains four .gif files and
one .class file. The last entry is a Bean.
Name: sunw/demo/slides/slide0.gif
Name: sunw/demo/slides/slide1.gif
Name: sunw/demo/slides/slide2.gif
Name: sunw/demo/slides/slide3.gif
Name: sunw/demo/slides/Slides.class
Java-Bean: True
A manifest file may reference several .class files. If a .class
file is a Java Bean, its entry must be immediately followed
by the line "Java-Bean: True".
A utility is used to generate a JAR file. Its
syntax is shown here:
jar options files
jar tf Xyz.jar
Introspection
Introspection is the process of analyzing a Bean to
determine its capabilities. This is an essential feature of
the Java Beans API, because it allows an application builder
tool to present information about a component to a
software designer.
There are two ways in which the developer of a Bean can
indicate which of its properties, events, and methods
should be exposed by an application builder tool.
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 is provided that
explicitly supplies this information.
The first 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. This
section discusses three types of properties:
simple, Boolean, and indexed.
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);
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.
The following listing shows a class that has three read/write simple properties:
c:\\bdk\\demo\\sunw\\demo\\colors.
Then change to that directory.
Name: sunw/demo/colors/Colors.class
Java-Bean: True
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.
To implement a bound property, take the following steps:
Import the java.beans package. This gives you access to the
PropertyChangeSupport class.
This object maintains the property change listener list and fires
property change events. You can also make your class a
PropertyChangeSupport subclass.
button.addPropertyChangeListener(aButtonListener);
EG:- The TickTock Bean is supplied with the BDK. It generates a property
change event every N seconds. N is a property of the Bean that can be
changed via the Properties window of the BDK. The next example builds
an application that uses the TickTock Bean to automatically control the
Colors Bean.
STEPS
start the BDK and create an instance of the Colors Bean in the BeanBox
window.
Create an instance of the TickTock Bean. The Properties window should
show one property for this component. It is "Interval" and its initial value
is 5. This represents the number of seconds that elapse between property
change events generated by the TickTock Bean. Change the value to 1.
Now you need to map events generated by the TickTock Bean into
method calls on the Colors Bean. Follow these steps:
1. Go to the menu bar of the BeanBox and select Edit | Events |
propertyChange | propertyChange. You should 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. You should see the Event Target Dialog dialog
box.
3. The dialog box allows you to choose a method that should be invoked
when this event occurs. Select the entry labeled "change" and click the OK
button. You should see a message box appear very briefly, stating that
the tool is "Generating and compiling adaptor class."
A Bean property is constrained when any change
to that property can be vetoed. Usually an
outside object exercises the right to veto, but the
Bean itself can also veto a property change.
There are three parts to constrained property
implementations:
A source Bean containing one or more constrained
properties.
Listener objects that implement the
VetoableChangeListener interface. This object accepts or
rejects proposed changes to a constrained property in
the source Bean.
A PropertyChangeEvent object containing the property
name, and its old and new values. This is the same class
used for bound properties.
A Bean containing constrained properties must
Allow VetoableChangeListener objects to register and
unregister their interest in receiving notification that a
property change is proposed.
Fire property change events at those interested listeners
when a property change is proposed. The event should
be fired before the actual property change takes place.
This gives each listener a chance to veto the proposed
change. The PropertyChangeEvent is fired by a call to
each listeners vetoableChange method.
If a listener vetoes, then make sure that any other
listeners can revert to the old value. This means
reissuing the vetoableChange call to all the listeners,
with a PropertyChangeEvent containing the old value.
The VetoableChangeSupport utility class is provided to implement these
capabilities. This class implements methods to add and remove
VetoableChangeListener objects to a listener list, and a method that fires
property change events at each listener in that list when a property
change is proposed. This method will also catch any vetoes, and resend
the property change event with the original property value. Your Bean
can either inherit from VetoableChangeSupport, or use an instance of it.
Note that, in general, constrained properties should also be bound
properties. When a constrained property change does occur, a
PropertyChangeEvent can be sent via
PropertyChangeListener.propertyChange to signal all
VetoableChangeListener Beans that the change has taken effect. This
lets all the vetoable change listeners know that the change was not
vetoed by any listener.
Here's the steps to implement constrained properties in your Beans:
1) Import the java.beans package. This gives you access to the
VetoableChangeSupport class.
2) Instantiate a VetoableChangeSupport object within your Bean:
private VetoableChangeSupport vetos = new
VetoableChangeSupport(this);
}
So to make your class able to listen and respond
to property change events, your listener class
must:
Class Description
BeanDescriptor This class provides information about a Bean. It
also allows you to associate a customizer with a
Bean.
Beans This class is used to obtain information about a
Bean.
EventSetDescriptor Instances of this class describe an event that can
be generated by a Bean.
Class Description
FeatureDescriptor This is the superclass of the
PropertyDescriptor, EventSetDescriptor, and
MethodDescriptor classes.
IndexedPropertyDescr Instances of this class describe an indexed
iptor property of a Bean.
IntrospectionExceptio An exception of this type is generated if a
n problem occurs w hen analyzing a Bean.
Introspector This class analyzes a Bean and constructs a
BeanInfo object that describes the component.
MethodDescriptor Instances of this class describe a method of a
Bean.
ParameterDescriptor Instances of this class describe a method
parameter
PropertyChangeEvent This event is generated when bound or
constrained properties are changed. It is sent to
objects that registered an interest in these
events and implement either the
PropertyChangeListener or
VetoableChangeListener
interfaces.
PropertyDescriptor Instances of this class describe a property of a
Bean.
SimpleBeanInfo This class provides functionality that can be
used when writing BeanInfo classes.
VetoableChangeSupp Beans that support constrained properties can
ort use this class to notify VetoableChangeListener
objects.