420 likes | 673 Views
JavaBeans. Chapter 8. Objectives. Learn what the JavaBeans component model is Understand Bean Development environments Use the Sun BeanBox Create a JavaBean class. Objectives (Cont.). Explore JavaBean property types Add custom event types to your beans Create a JavaBean class with events
E N D
JavaBeans Chapter 8 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
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
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
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
Elements of a JavaBean Interface Java Programming: Advanced Topics
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
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
The BDK BeanBox Properties Window Java Programming: Advanced Topics
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
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
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
Indexed Properties (Cont.) 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
Bound Properties (Cont.) Java Programming: Advanced Topics
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
Constrained Properties (Cont.) Java Programming: Advanced Topics
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
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 PropertyChangeEventobjects Java Programming: Advanced Topics
Custom Event Class Java Programming: Advanced Topics
Custom Event Class (Cont.) Java Programming: Advanced Topics
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
JavaBean Information Classes Java Programming: Advanced Topics
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
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
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
JavaBean Class Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) Java Programming: Advanced Topics
JavaBean Class (Cont.) 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
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