Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: Advanced Topics 1 JavaBeans Chapter 8.

Similar presentations


Presentation on theme: "Java Programming: Advanced Topics 1 JavaBeans Chapter 8."— Presentation transcript:

1 Java Programming: Advanced Topics 1 JavaBeans Chapter 8

2 Java Programming: Advanced Topics 2 Objectives Learn what the JavaBeans component model is Understand Bean Development environments Use the Sun BeanBox Create a JavaBean class

3 Java Programming: Advanced Topics 3 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

4 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

5 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

6 Java Programming: Advanced Topics 6 Elements of a JavaBean Interface

7 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

8 Java Programming: Advanced Topics 8 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

9 Java Programming: Advanced Topics 9 The BDK BeanBox Properties Window

10 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

11 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

12 Java Programming: Advanced Topics 12 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

13 Java Programming: Advanced Topics 13 Indexed Properties (Cont.)

14 Java Programming: Advanced Topics 14 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

15 Java Programming: Advanced Topics 15 Bound Properties (Cont.)

16 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

17 Java Programming: Advanced Topics 17 Constrained Properties (Cont.)

18 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

19 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

20 Java Programming: Advanced Topics 20 Custom Event Class

21 Java Programming: Advanced Topics 21 Custom Event Class (Cont.)

22 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

23 Java Programming: Advanced Topics 23 JavaBean Information Classes

24 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()

25 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

26 Java Programming: Advanced Topics 26 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

27 Java Programming: Advanced Topics 27 JavaBean Class

28 Java Programming: Advanced Topics 28 JavaBean Class (Cont.)

29 Java Programming: Advanced Topics 29 JavaBean Class (Cont.)

30 Java Programming: Advanced Topics 30 JavaBean Class (Cont.)

31 Java Programming: Advanced Topics 31 JavaBean Class (Cont.)

32 Java Programming: Advanced Topics 32 JavaBean Class (Cont.)

33 Java Programming: Advanced Topics 33 JavaBean Class (Cont.)

34 Java Programming: Advanced Topics 34 JavaBean Class (Cont.)

35 Java Programming: Advanced Topics 35 JavaBean Class (Cont.)

36 Java Programming: Advanced Topics 36 JavaBean Class (Cont.)

37 Java Programming: Advanced Topics 37 JavaBean Class (Cont.)

38 Java Programming: Advanced Topics 38 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

39 Java Programming: Advanced Topics 39 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


Download ppt "Java Programming: Advanced Topics 1 JavaBeans Chapter 8."

Similar presentations


Ads by Google