Download Chap. 8 - kuroski.net

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
JavaBeans
Chapter 8
Java Programming: Advanced Topics
1
Objectives
• Learn what the JavaBeans component
model is
• Understand Bean Development
environments
• Use the Sun BeanBox
• Create a JavaBean class
Java Programming: Advanced Topics
2
Objectives (Cont.)
•
•
•
•
Explore JavaBean property types
Add custom event types to your beans
Create a JavaBean class with events
Supply additional information and
support classes for a JavaBean
Java Programming: Advanced Topics
3
JavaBeans Component Model
• The JavaBeans component model is a
framework for creating reusable Java classes
• Classes that conform to the JavaBeans
specification can be loaded into development
tools called beanboxes, with which developers
can create applications by constructing them
from parts
• JavaBeans are packaged in .jar files that include
a manifest file
Java Programming: Advanced Topics
4
What Makes a Class a Bean
• There is no common superclass that all
JavaBean classes extend.
• To define a JavaBean follow the programming
conventions
• All beans should have a constructor that takes
no arguments because the Sun BeanBox call
this constructor
• A JavaBean class must implement the marker
interface Serializable, because beanboxes use
serialization to save the state of beans
Java Programming: Advanced Topics
5
Elements of a JavaBean Interface
Java Programming: Advanced Topics
6
Bean Development Environments
• Sun created a demonstration development
environment called the BeanBox
• The Bean Builder is a program that
demonstrates new and emerging
technologies within the Java platform that
allow the construction of applications using
component assembly mechanisms
Java Programming: Advanced Topics
7
Using the Sun BeanBox
• When a bean is instantiated in a beanbox, the
bean’s methods are called in the following
order:
– The constructor with no arguments is called to set
up the bean
– The preferredSize method returns the display
dimensions of the bean
– The paint method draws the bean on the BeanBox
window
Java Programming: Advanced Topics
8
The BDK BeanBox Properties
Window
Java Programming: Advanced Topics
9
Creating a JavaBean Class
• Create a JavaBean class
• Package your JavaBean classes into a .jar file
• Load your .jar file into the BeanBox to connect
with other JavaBean components
Java Programming: Advanced Topics
10
Exploring JavaBean Property
Types
• Properties are the attributes of a bean,
commonly implemented as the fields of a Java
class
• There are four types of JavaBean Property
Types:
–
–
–
–
Simple
Indexed
Bound
Constrained
Java Programming: Advanced Topics
11
Indexed Properties
• Properties can be indexed under a single
name with an integer index value
• Indexed properties are arrays of values
• The mutator and accessor methods for an
indexed property must have the arguments,
names, and return types
Java Programming: Advanced Topics
12
Indexed Properties (Cont.)
Java Programming: Advanced Topics
13
Bound Properties
• Bound properties provide notification when they
change so that other JavaBeans can listen for
these changes and act accordingly
• The package java.beans includes a class for use
with bound properties, PropertyChangeSupport
Java Programming: Advanced Topics
14
Bound Properties (Cont.)
Java Programming: Advanced Topics
15
Constrained Properties
• Constrained properties: bound properties with
the additional characteristics that other listeners
can prevent a change in value from occurring
• To implement a constrained property, a
JavaBean class should use an object of the
VetoableChangeSupport class
• Each listener can veto a change and stop it from
happening
Java Programming: Advanced Topics
16
Constrained Properties (Cont.)
Java Programming: Advanced Topics
17
Adding Custom Event Types
• To create and use a custom event:
– Define the event class X that extends
java.util.EventObject or one of its subclasses
– Define the interface, XListener, that the event
listeners must implement
– Define the methods addXListener and
removeXListener for the JavaBean class that can
fire the event
– The JavaBean class should define a fireX method
Java Programming: Advanced Topics
18
Creating a JavaBean with Events
• Event handling follows the same model as event
handling for components in the Swing and AWT
APIs
• If a bean can generate an event Y, the class for
the event is YEvent
• A listener class YListener should handle YEvent
objects
• Changes to properties trigger events of
PropertyChangeEvent objects
Java Programming: Advanced Topics
19
Custom Event Class
Java Programming: Advanced Topics
20
Custom Event Class (Cont.)
Java Programming: Advanced Topics
21
Using the BeanInfo Classes
• Beanboxes use the Reflection API to determine
what they need to know about a JavaBean
• BeanInfo classes are used only when beans are
being connected in a beanbox
• The BeanInfo classes implement the
java.beans.BeanInfo interface
• If the Reflection API cannot provide all the
information that a beanbox needs about a bean,
you can supply an additional information class
that implements the interface BeanInfo
Java Programming: Advanced Topics
22
JavaBean Information Classes
Java Programming: Advanced Topics
23
Using the BeanInfo Classes
(Cont.)
• Methods of the interface java.beans.BeanInfo:
–
–
–
–
–
–
–
–
BeanInfo[ ] getAdditionalBeanInfo()
BeanDescriptor getBeanDescriptor()
int getDefaultEventIndex()
int getDefaultPropertyIndex()
EventSetDescriptor[ ] getEventSetDescriptors()
Image getIcon( int iconKind )
MethodDescriptor[ ] getMethodDescriptors()
PropertyDescriptor[ ] getPropertyDescriptors()
Java Programming: Advanced Topics
24
Providing a Custom Property
Editor
• Beanboxes typically provide property editors for
properties of types String, Font, and Color
• You can provide customized editors for other
kinds of properties by defining a class that
extends PropertyEditorSupport or implements
the PropertyEditor interface
• If the property editor dialog box of the beanbox
is not adequate, you can supply a customizer
class for the bean
Java Programming: Advanced Topics
25
A Customizer Class
• The customizer class must be a component
that can be embedded in a dialog box
• The class must have a constructor that has no
arguments
• When you create a customizer class, you must
provide the method getBeanDescriptor in the
BeanInfo class associated with the JavaBean
Java Programming: Advanced Topics
26
JavaBean Class
Java Programming: Advanced Topics
27
JavaBean Class (Cont.)
Java Programming: Advanced Topics
28
JavaBean Class (Cont.)
Java Programming: Advanced Topics
29
JavaBean Class (Cont.)
Java Programming: Advanced Topics
30
JavaBean Class (Cont.)
Java Programming: Advanced Topics
31
JavaBean Class (Cont.)
Java Programming: Advanced Topics
32
JavaBean Class (Cont.)
Java Programming: Advanced Topics
33
JavaBean Class (Cont.)
Java Programming: Advanced Topics
34
JavaBean Class (Cont.)
Java Programming: Advanced Topics
35
JavaBean Class (Cont.)
Java Programming: Advanced Topics
36
JavaBean Class (Cont.)
Java Programming: Advanced Topics
37
Summary
• The JavaBeans component model is a
framework for creating reusable Java classes
• Classes that conform to the JavaBeans
specification can be loaded into development
tools called beanboxes
• A bean must implement the interface
java.io.Serializable, and must have a
constructor that has no arguments
• Properties are the attributes of a bean, and can
be single entities, indexed, bound or
constrained
Java Programming: Advanced Topics
38
Summary (Cont.)
• JavaBean objects use event-handling
mechanism to notify other JavaBean objects that
some event has occurred
• Changes to properties trigger events of
PropertyChangeEvent objects
• Additional information classes, BeanInfo classes,
can accompany a JavaBean class
• You can provide customized editors by defining a
class that extends PropertyEditorSupport or
implements the PropertyEditor interface
Java Programming: Advanced Topics
39