You are on page 1of 36

1

JavaBEANS ver1.0

Introduction to Java BEANS


JavaBeans takes Java's "Write Once, Run Anywhere" capability and extends it to include "reuse everywhere". JavaBeans is a portable, platform-independent component model, written in Java. Small, reusable, software components can be created using Java Beans. A visual "builder" program combines components from disparate sources to create applications quickly and easily.
2 JavaBEANS ver1.0

What's a BEAN
Beans are independent, reusable software modules. Beans may be visible objects, like AWT components, or invisible objects, like queues and stacks. A builder/integration tool manipulates Beans to create applets and applications.

JavaBEANS ver1.0

Beans Architecture
Beans consist of three things: Events Properties Methods Also, since Beans rely on their state, they need to be able to be persistent over time

JavaBEANS ver1.0

Events

Events can be defined apart from AWT events for specifying change of state of a bean.

Beans can send customized events to the respective listeners. These events can be further handled by listeners. Steps to customize events define a class to describe the event.This class must extend EventObject. define an interface for listeners to receive this event.This interface must extend EventListener.
5 JavaBEANS ver1.0

Events contd..
Source class must provide methods that allow listeners to register and unregister for event notifications. Source must provide code to generate the event and send it to all registered listeners. Listener class must implement the interface to receive the event. Listener must register/unregister to receive notification.
6 JavaBEANS ver1.0

Properties
A property is a public attribute of the Bean, usually represented by a non-public instance variable. It can be read-write, read-only, or write-only. There are four different types of properties :
Simple Indexed Bound Constrained

JavaBEANS ver1.0

Simple Properties
To create a simple property,define a pair of set/get routines.

Whatever name used in the pair of routines, becomes the property name (no matter what instance variable name you use)

JavaBEANS ver1.0

Simple Properties contd..


For instance, to define a property salary for an Employee Bean : float salary; public void setSalary (float newSalary) { salary = newSalary; } public float getSalary () { return salary; }

JavaBEANS ver1.0

Simple Properties contd..


If you need a read-only property, only define a getPropertyName routine.

If you want a write-only property, only define a setPropertyName routine.

10

JavaBEANS ver1.0

Simple Properties contd..


Boolean properties can change their get routine to an isPropertyName routine: boolean trained; public void setTrained (boolean trained){ this.trained = trained; } public boolean isTrained () { return trained; }
11 JavaBEANS ver1.0

Indexed Properties
An indexed property is for when a single property can hold an array of values. The design pattern for these properties is :
public void setPropertyName (PropertyType[] list) public void setPropertyName (PropertyType element, int position) public PropertyType[] getPropertyName () public PropertyType getPropertyName (int position)
12 JavaBEANS ver1.0

Bound Properties
Property of a bean whose change sends notification to other objects. Bean containing this property must maintain property change listeners and alert them when the property changes. Java.beans package defines-PropertyChangeSupport class PropertyChangeEvent class PropertyChangeListener interface
13 JavaBEANS ver1.0

PropertyChangeSupport class
This class can be used to perform much of the work associated with bound properties. Object of this class maintains list of property change listeners and fires property change events. Bean can subclass this or create an object of this class. Methods in this class void addPropertyChangeListener(PropertyChangeListener) void removePropertyChangeListener ( PropertyChangeListener) void firePropertyChange(String pname,Object oldvalue,Object newvalue)
14 JavaBEANS ver1.0

PropertyChangeEvent class

Methods in this class Object getOldValue() Object getNewValue() String getPropertyName() Object of this class is sent to all listeners

15

JavaBEANS ver1.0

PropertyChangeListener interface
Used to receive PropertyChangeEvent notifications. Defines one method void propertyChange(PropertyChange event)

16

JavaBEANS ver1.0

Constrained Property
Property of a bean whose change sends notification to other objects. Listener object has right to veto this change. Source receives exception if listener object vetoes the change and restores old value. Java.beans package defines-VetoableChangeSupport class PropertyVetoException class VetoableChangeListener interface
17 JavaBEANS ver1.0

VetoableChangeSupport class
This class can be used to perform much of the work associated with constrained properties. Object of this class maintains list of property change listeners and fires property change events. Bean can subclass this or create an object of this class. Methods in this class void addVetoableChangeListener(VetoableChangeListener) void removeVetoableChangeListener( VetoableChangeListener) void fireVetoableChange(String pname,Object oldValue,Object newValue)
18 JavaBEANS ver1.0

PropertyVetoException class
Exception to be thrown by listener objects Has one constructor
PropertyVetoException(String msg, PropertyChangeEvent)

19

JavaBEANS ver1.0

VetoableChangeListener interface
Used to receive PropertyChangeEvent notifications Defines one method
void vetoableChange(PropertyChangeEvent) throws PropertyVetoException

20

JavaBEANS ver1.0

Property Editors
Customized property editors can be defined for properties of a bean for which there is no default editor support. PropertyEditor interface has number of methods for this purpose. PropertyEditorSupport class provides default implementation for this interface.

21

JavaBEANS ver1.0

PropertyEditor interface
Methods void addPropertyChangeListener(propertyChangeListener) String getAsText() void removePropertyChangeListener( propertyChangeListen er) void setAsText(String) Component getCustomEditor() boolean supportsCustomEditor() PropertyEditorSupport Constructor: ProtectedPropertyEditorSupport(Object)
22 JavaBEANS ver1.0

Methods
Bean methods are available for anyone to call by just making each public. However, one can restrict which methods are visible to the Bean builder/integration tool by providing a getMethodDescriptors method along with your Bean's BeanInfo. Every Bean can provide a supporting BeanInfo class to customize a Bean's appearance to an integration tool.
23 JavaBEANS ver1.0

Persistence
Persistence is the ability of an object to store its state, for recreation later. Beans use Java's object serialization capabilities for persistence.

24

JavaBEANS ver1.0

Reflection
Reflection is the ability to obtain information about the fields,constructors and methods of a class. Java.lang package provides Class class having more than 30 different methods that allow to get information about any object.

25

JavaBEANS ver1.0

Java.lang.reflect package
Java.lang.reflect package has -Member interface Field class Constructor class Method class Modifier class

26

JavaBEANS ver1.0

Introspection

Ability to obtain information about the properties,events and methods of a bean. This feature is used by builder tools. It provides the data needed by developers to use and connect Beans.

27

JavaBEANS ver1.0

BeanInfo.class
Introspection mechanism searches for XyzBeanInfo.class for a Xyz.class If such class exists,it is used to provide information about the bean. This class can be created to customize information to be provided to the user.

28

JavaBEANS ver1.0

Java.beans.package
Introspector class BeanInfo class SimpleBeanInfo class FeatureDescriptor class BeanDescriptor class EventSetDescriptor class MethodDescriptor class PropertyDescriptor class
29 JavaBEANS ver1.0

BeanInfo interface
Defines a set of methods and constants for introspection SimpleBeanInfo class provides default implementation of this interface. Methods BeanDescriptor getBeanDescriptor() EventSetDescriptor[] getEventDescriptor() MethodDescriptor[] getmethodDescriptors() PropertyDescriptor[]getPropertyDescriptors()
30 JavaBEANS ver1.0

FeatureDescriptor class
Super class for all descriptor classes. Methods void setDisplayName(String s) String getDisplayName() void setName(String s) String getName() Object getvalue() ..................
31 JavaBEANS ver1.0

BeanDescriptor class

This class associates customizer with a Bean. A customizer provides GUI through which user modifies the properties of the Bean.

32

JavaBEANS ver1.0

EventSetDescriptor class
Describes a set of events generated by a Bean. Constructors
EventSetDescriptor(Class source,String esname,Class listener,String listenermethodname) EventSetDescriptor(Class source,String esname,Class listener,String addListener,String[]listenermethodnames, String addListenermethodName,String removeListenermethodname)
33 JavaBEANS ver1.0

PropertyDescriptor class
Describes a property of a Bean. Constructors PropertyDescriptor(String pname,Class cls) PropertyDescriptor(String pname,Class cls,String getMethodname,String setMethodname) Methods Class getPropertyEditorClass() Method getReadmethod() Method getWritemethod()

34

JavaBEANS ver1.0

Technology Comparison to ActiveX/COM


JavaBeans is a framework for building applications out of Java components (Beans). ActiveX is a framework for building compound documents with ActiveX controls. A Bean is very similar to an ActiveX control. However, a Bean is written in Java, so has Java's security and cross platform nature. ActiveX controls written in Java is possible, however they require a port of Microsoft's Common Object Model (COM) to be used outside of Microsoft Windows.
35 JavaBEANS ver1.0

JavaBeans Benefit Analysis


Write Once, Run Anywhere java.beans package part of Core API Component Reusability Reuse Everywhere -across platforms/tools/solutions Example: 3D Charting Bean - drop into any container, regardless of platform tool Interoperability Communicate with other component architectures Beans-ActiveX Bridge now beta, Beans-OpenDoc under development
36 JavaBEANS ver1.0

You might also like