Professional Documents
Culture Documents
Introduction to JavaBeans
A first definition of what JavaBeans are:
A JavaBean is a reusable software
component that can be manipulated visually
in a builder tool.
Definition of a software component:
Software components are self-contained
software units developed according to the
motto.
Developed them once, run and reused
them everywhere. Or in other words,
reusability is the main concern behind the
component model.
Objectives
Notion of component on top of Java.
Designed mostly for the construction of
graphical user interface
The goal of the Java Beans APIs is to
define a software component model for
Java.
components that can be composed
together into applications by end users.
Features
Unifying features of Java Bean component :
support for introspection
so that a builder tool can analyze how a bean
works
support for customization
so that when using a builder tool a user can
customize the appearance and behaviour of a bean
support for events
as a simple communication metaphor that can be
used to connect up beans
support for properties
both for customization and for programmatic use
support for persistence
so that a bean can be customized in builder tool
and that have its customized state saved away and
reloaded later
Characteristics
Its capability spans from simple
functions to complex functions.
It may be visible or invisible to a
user.
It may work autonomously on a user
computer or it may cooperate on
some task with other components.
/* file Thermometer.java */
public class Thermometer {
/* these two functions define the
"currentTemperature" property */
public int getCurrentTemperature() { return
temp; }
public void setCurrentTemperature(int aTemp)
{ temp = aTemp; }
private int temp;
public void
temperatureChanged(TempChangedEvent ev)
{ ... } ... other implementation details ... }
Invoking methods
In a bean component, various
methods can be invoked for
executing code.
In order to connect two components,
a mapping must be done between
the event generated by the quote
bean and appropriate
method
of
the
Graph
Quote
Bean
graphBean
bean.
Installation(cont)
C:\beans
Bean box
Run.bat
Instrospection
builder tools typically provide a property
sheet where one can conveniently set the
properties of a JavaBean component.
In order to provide this service a builder tool
needs to examine the component for its
features (=properties, events and methods).
This process is referred to as introspection.
To obtain information about a specific
JavaBean one can use the static
getBeanInfo() method of the Introspector
class.
import
import
import
import
java.beans.BeanInfo;
java.beans.Introspector;
java.beans.IntrospectionException;
java.beans.PropertyDescriptor;
BeanInfo Interface
A bean implementor who wishes to
provide explicit information about their
bean may provide a BeanInfo class that
implements this BeanInfo interface and
provides explicit information about the
methods, properties, events, etc, of their
bean.
Method Detail
getBeanDescriptor
public BeanDescriptor
getBeanDescriptor()
Gets the beansBeanDescriptor.
Returns:A BeanDescriptor providing
overall information about the bean
getEventSetDescriptors
public EventSetDescriptor[]
getEventSetDescriptors()
getDefaultEventIndex
public int getDefaultEventIndex()
A bean may have a "default" event that
is the event that will mostly commonly
be used by humans when using the bean.
Returns:Index of default event in the
EventSetDescriptor array returned by
getEventSetDescriptors.Returns -1 if
there is no default event.
getPropertyDescriptors
public PropertyDescriptor[]
getPropertyDescriptors()
getDefaultPropertyIndex
public int getDefaultPropertyIndex()
A bean may have a "default" property that
is the property that will mostly commonly
be initially chosen for update by human's
who are customizing the bean.
Returns:Index of default property in the
PropertyDescriptor array returned by
getPropertyDescriptors.Returns -1 if there is
no default property.
getMethodDescriptors
public MethodDescriptor[]
getMethodDescriptors()
getAdditionalBeanInfo
public BeanInfo[]
getAdditionalBeanInfo()
that provide additional information on
the current bean
getIcon
public Image getIcon(inticonKind)
This method returns an image object
that can be used to represent the
bean in toolboxes
Properties
There exist four different kinds of
properties a JavaBean can expose:
1.Simple
2.Indexed
3.Bound
4.Constrained
All types of property have in common
that they are characterized by a pair
of set/get methods.
Simple Properties
As the name suggests, simple
properties are the simplest of the
four.
we would like to have a default font
size, which will be used as initial font
size at runtime.
Indexed Properties
If a simple property can hold an array
of value they are no longer called
simple but instead indexed properties
An indexed property may expose
set/get methods to read/write one
element in the array and/or so-called
array getter/setter which read/write
the entire array.
T getN(int index);
void setN(int index, T value);
T[ ] getN( );
void setN(T values[ ]);
JAR Files
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.
Manifest Files
A developer must provide a manifest
file to indicate which of the components
in a JAR
file are Java Beans.
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.
The JAR Utility
A utility is used to generate a JAR file.
Its syntax is shown here:
jar options files
// A simple Bean.
package sunw.demo.colors;
import java.awt.*;
import java.awt.event.*;
public class Colors extends Canvas {
transient private Color color;
private boolean rectangular;
public Colors() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
change();
}
});
rectangular = false;
setSize(200, 100);
change();
}
public boolean getRectangular() {
return rectangular;
}
public void setRectangular(boolean flag) {
this.rectangular = flag;
repaint();
}
public void change() {
color = randomColor();
repaint();
}
private Color randomColor() {
int r = (int)(255*Math.random());
int g = (int)(255*Math.random());
int b = (int)(255*Math.random());
return new Color(r, g, b);
}
public void paint(Graphics g) {
Dimension d = getSize();
int h = d.height;
int w = d.width;
g.setColor(color);
if(rectangular) {
g.fillRect(0, 0, w-1, h-1);
}
else {
g.fillOval(0, 0, w-1, h-1);
}
}
}
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.
The TickTock Bean is supplied with the
BDK. It generates a property change
event
every N seconds.
public void
addPropertyChangeListener(PropertyChangeListener l)
{ changes.addPropertyChangeListener(l); }
public void
removePropertyChangeListener(PropertyChangeListener
l)
{ changes.removePropertyChangeListener(l); }
4.Modify a property's setter method to fire a
property change event when the property is
changed.
public void setLabel(String newLabel)
{
String oldLabel = label;
label = newLabel;
sizeToFit();
changes.firePropertyChange("label", oldLabel, newLabel);
}
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.
Those other objects have the ability to
veto the proposed change. This capability
allows a Bean to operate differently
according to its run-time environment.
Persistence
Persistence is the ability to save a Bean to
nonvolatile storage and retrieve it at a later
time.
Inorder to do this we take the help of BDK.
Saving the beans in the BDK :choose
file and then the save options.
Restoring the beans in the BDK:choose
file and then load options.
Interface
Description
AppletInitializer
BeanInfo
Customizer
ExceptionListener
PropertyChangeListener
PropertyEditor
Interface
Description
VetoableChangeListener
Visibility
Class
Description
Encoder
EventHandler
EventSetDescriptor
FeatureDescriptor
IndexedPropertyDescriptor
SimpleBeanInfo
Class
description
IntrospectionException
Introspector
MethodDescriptor
ParameterDescriptor
PropertyChangeEvent
PropertyDescriptor
PropertyEditorManager