Download presentation
Presentation is loading. Please wait.
1
Java Programming: Advanced Topics
JavaBeans Chapter 8 Java Programming: Advanced Topics
2
Java Programming: Advanced Topics
Objectives Learn what the JavaBeans component model is Understand Bean Development environments Use the Sun BeanBox Create a JavaBean class Java Programming: Advanced Topics
3
Java Programming: Advanced Topics
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
4
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
5
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
6
Elements of a JavaBean Interface
Java Programming: Advanced Topics
7
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
8
Java Programming: Advanced Topics
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
9
The BDK BeanBox Properties Window
Java Programming: Advanced Topics
10
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
11
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
12
Java Programming: Advanced Topics
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
13
Indexed Properties (Cont.)
Java Programming: Advanced Topics
14
Java Programming: Advanced Topics
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
15
Bound Properties (Cont.)
Java Programming: Advanced Topics
16
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
17
Constrained Properties (Cont.)
Java Programming: Advanced Topics
18
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
19
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
20
Java Programming: Advanced Topics
Custom Event Class Java Programming: Advanced Topics
21
Custom Event Class (Cont.)
Java Programming: Advanced Topics
22
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
23
JavaBean Information Classes
Java Programming: Advanced Topics
24
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
25
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
26
Java Programming: Advanced Topics
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
27
Java Programming: Advanced Topics
JavaBean Class Java Programming: Advanced Topics
28
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
29
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
30
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
31
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
32
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
33
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
34
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
35
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
36
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
37
Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
38
Java Programming: Advanced Topics
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
39
Java Programming: Advanced Topics
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.