javabeans n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
JavaBeans PowerPoint Presentation
Download Presentation
JavaBeans

Loading in 2 Seconds...

play fullscreen
1 / 39

JavaBeans - PowerPoint PPT Presentation


  • 158 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

JavaBeans


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
javabeans

JavaBeans

Chapter 8

Java Programming: Advanced Topics

objectives
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
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
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
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
Elements of a JavaBean Interface

Java Programming: Advanced Topics

bean development environments
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
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
The BDK BeanBox Properties Window

Java Programming: Advanced Topics

creating a javabean class
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
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
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
Indexed Properties (Cont.)

Java Programming: Advanced Topics

bound properties
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
Bound Properties (Cont.)

Java Programming: Advanced Topics

constrained properties
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
Constrained Properties (Cont.)

Java Programming: Advanced Topics

adding custom event types
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
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
Custom Event Class

Java Programming: Advanced Topics

custom event class cont
Custom Event Class (Cont.)

Java Programming: Advanced Topics

using the beaninfo classes
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
JavaBean Information Classes

Java Programming: Advanced Topics

using the beaninfo classes cont
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
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
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
JavaBean Class

Java Programming: Advanced Topics

javabean class cont
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont1
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont2
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont3
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont4
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont5
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont6
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont7
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont8
JavaBean Class (Cont.)

Java Programming: Advanced Topics

javabean class cont9
JavaBean Class (Cont.)

Java Programming: Advanced Topics

summary
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
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