Professional Documents
Culture Documents
JavaBeans
JavaBeans™ is a portable, platform-independent component
model written in the Java programming language. The JavaBeans
architecture was built through a collaborative industry effort and
enables developers to write reusable components in the Java
programming language.
JavaBeans Concepts
The JavaBeans™ architecture is based on a component model
which enables developers to create software units called components.
Components are self-contained, reusable software units that can be
visually assembled into composite components, applets, applications,
and servlets using visual application builder tools. JavaBean
components are known as beans.
1
2
Beans vary in functionality and purpose. You have probably met some
of the following beans in your programming practice:
• Animation applet
• Spreadsheet application
2
3
Your bean will be named SimpleBean. Here are the steps to create it:
<project default="build">
3
4
<target name="compile">
<javac destdir="${basedir}">
<src location="${basedir}"/>
</javac>
</target>
<target name="clean">
<delete file="${jarfile}">
<fileset dir="${basedir}" includes="*.class"/>
</delete>
</target>
</project>
It is recommended to save an XML script in the build.xml file,
because Ant recognizes this file name automatically.
3. Load the JAR file. Use the NetBeans IDE GUI Builder to load
the jar file as follows:
Start NetBeans.
4
5
Specify a location for your SimpleBean JAR file and follow the
Add from JAR Wizard instructions.
5
6
Lesson: Properties
In the following sections you will learn how to implement bean
properties. A bean property is a named attribute of a bean that can
affect its behavior or appearance. Examples of bean properties include
color, label, font, font size, and display size.
o Standard
o Expert
o Preferred
• Hidden – A bean property that can be changed. However, these properties are not
disclosed with the BeanInfo class
BeanBuilder uses this schema to group and represent properties in the
Properties window.
6
7
Simple Properties
To add simple properties to a bean, add appropriate getXXX and setXXX
methods (or isXXX and setXXX methods for a boolean property).
Bound Properties
The accessor methods for a bound property are defined in the same
way as those for simple properties. However, you also need to provide
the event listener registration methods forPropertyChangeListener classes
and fire a PropertyChangeEvent (in the API reference documentation)
event to the PropertyChangeListener objects by calling their propertyChange
methods
7
8
Constrained Properties
8
9
Handling Vetoes
If a registered listener vetoes a proposed property change by throwing
a PropertyVetoException exception, the source bean with the constrained
property is responsible for the following actions:
• Catching exceptions.
9
10
Indexed Properties
An indexed property is an array of properties or objects that supports
a range of values and enables the accessor to specify an element of a
property to read or write.
Manipulating Events
The source of an event is the object that originates or fires the event.
The source must define the events it will fire, as well as the methods
for registering listeners of those events. A listener is an object that
indicates that it is to be notified of events of a particular type.
Listeners register for events using the methods defined by the sources
of those events.
10
11
From the Properties lesson you discovered two event listeners. The
PropertyChangeListener(in the API reference documentation) interface
provides a notification whenever a bound property value is changed
and the VetoableChangeListener(in the API reference documentation)
creates a notification whenever a bean changes a constrained property
value.
Simple Event Example
This example represents an application that performs an action when a
button is clicked. Button components are defined as sources of an
event type called ActionEvent(in the API reference documentation).
Listeners of events of this type must register for these events using
the addActionListener method.
Therefore, the addActionListener method is used to register the
ButtonHandler object as a listener of the ActionEvent event that is fired by
the button.
/**
* When receives action event notification, appends
* message to the JTextArea passed into the constructor.
*/
public void actionPerformed( ActionEvent event )
{
11
12
class ActionTester {
public static void main(String args[]) {
JFrame frame = new JFrame( "Button Handler" );
JTextArea area = new JTextArea( 6, 80 );
JButton button = new JButton( "Fire Event" );
button.addActionListener( new ButtonHandler( area ) );
frame.add( button, BorderLayout.NORTH );
frame.add( area, BorderLayout.CENTER );
frame.pack();
frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
Bean Persistence
A bean has the property of persistence when its properties, fields, and
state information are saved to and retrieved from storage. Component
models provide a mechanism for persistence that enables the state of
components to be stored in a non-volatile place for later retrieval.
Any applet, application, or tool that uses that bean can then
"reconstitute" it by deserialization.
12
13
Controlling Serialization
You can control the level of serialization that your beans undergo.
Three ways to control serilization are:
13
14
Introspection
Introspection is the automatic process of analyzing a bean's design
patterns to reveal the bean's properties, events, and methods. This
process controls the publishing and discovery of bean operations and
properties. This lesson explains the purpose of introspection,
introduces the Introspection API, and gives an example of
introspection code.
Purpose of Introspection
A growing number of Java object repository sites exist on the Internet
in answer to the demand for centralized deployment of applets,
classes, and source code in general. Any developer who has spent time
hunting through these sites for licensable Java code to incorporate into
a program has undoubtedly struggled with issues of how to quickly and
cleanly integrate code from one particular source into an application.
14
15
Introspection API
The JavaBeans API architecture supplies a set of classes and interfaces
to provide introspection.
The BeanInfo (in the API reference documentation) interface of the
java.beans package defines a set of methods that allow bean
implementors to provide explicit information about their beans. By
specifying BeanInfo for a bean component, a developer can hide
methods, specify an icon for the toolbox, provide descriptive names for
properties, define which properties are bound properties, and much
more.
The getBeanInfo(beanName) (in the API reference documentation) of the
Introspector (in the API reference documentation) class can be used by
builder tools and other automated environments to provide detailed
information about a bean. The getBeanInfo method relies on the naming
conventions for the bean's properties, events, and methods. A call to
getBeanInfo results in the introspection process analyzing the bean’s
classes and superclasses.
The Introspector class provides descriptor classes with information about
properties, events, and methods of a bean. Methods of this class
locate any descriptor information that has been explicitly supplied by
the developer through BeanInfo classes. Then the Introspector class
applies the naming conventions to determine what properties the bean
has, the events to which it can listen, and those which it can send.
15
16
16
17
Property Editors
A property editor is a tool for customizing a particular property type.
Property editors are activated in the Properties window. This window
determines a property's type, searches for a relevant property editor,
and displays the property's current value in a relevant way.
17
18
gfx.setColor(color);
gfx.fillRect(box.x+1, box.y+1, box.width-4, box.height-4);
gfx.setColor(oldColor);
}
To support the custom property editor, override two more methods.
Override supportsCustomEditor to return true, and then override
getCustomEditor to return a custom editor instance.
ColorEditor.getCustomEditor returns this.
Customizers
You have learned that builder tools provide support for you to create
your own property editors. What other needs should visual builders
meet for complex, industrial-strength beans? Often it is undesirable to
have all the properties of a bean revealed on a single (sometimes
huge) property sheet. What if one single root choice about the type of
18
19
When you use a bean Customizer, you have complete control over how
to configure or edit a bean. A Customizer is an application that
specifically targets a bean's customization. Sometimes properties are
insufficient for representing a bean's configurable attributes.
Customizers are used where sophisticated instructions would be
needed to change a bean, and where property editors are too primitive
to achieve bean customization.
19