Java beans

3,124 views 39 slides Mar 24, 2018
Slide 1
Slide 1 of 39
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39

About This Presentation

about javabeans


Slide Content

JavaBeans Definition: A Java Bean is a reusable software component that can be manipulated visually in a builder tool. a software component model Software components are self-contained software units developed according to the motto. Developed them once, run and reused them everywhere.

A Java Bean is an ordinary java class that confirms the following rules It provides a default, no-argument constructor. It should be serializable and implement the Serializable interface. It may have a number of properties which can be read or written. It may have a number of "getter" and "setter" methods for accessing properties.

Advantages  of  Java  Beans Beans is platform independent, that means it can be run anywhere. Beans can work in different local platforms. Methods, properties and events of Beans can be controlled. It is easy to configure Java beans A bean can both receive and create events Configuration settings of a bean can be stored persistently and can be retrieved any time

How to write a java bean using coding standards public class StudentsBean implements java.io.Serializable { private String firstName = null; private String lastName = null; public StudentsBean () { } public String getFirstName () { return firstName ; } public String getLastName () { return lastName ; } public void setFirstName (String firstName ) { this.firstName = firstName ; } public void setLastName (String lastName ) { this.lastName = lastName ; } }

Bean Development Kit(BDK) BDK is a development tool through which java beans can be created, configured, executed or interconnected. Installation C:\beans Bean box Run.bat

Starting the BeanBox When you start the BeanBox, you'll see three windows: ToolBox window BeanBox window Properties window

The ToolBox window  displays the JavaBeans that are currently installed in the BeanBox The BeanBox window  itself appears initially as an empty window. the Properties window , displays the current properties for the selected Bean Method tracer , used to keep an eye on the activities of bean,mainly used for debugging purpose.

Create and Configure an Instance of the Molecule Bean Follow these steps Position the cursor on the ToolBox entry labeled Molecule and click the left mouse button. Move the cursor to the BeanBox display area and click the left mouse button. You can reposition the Molecule Bean by positioning the cursor over one of the hatched borders and dragging the Bean. You can change the molecule that is displayed by changing the selection in the Properties window

Instrospection builder tools typically provide a property sheet where one can conveniently set the properties of a JavaBean component. In order to provide this service a builder tool needs to examine the component for its features (=properties, events and methods).This process is referred to as “introspection”. Introspection is the automatic process of analyzing a bean's design patterns to reveal the bean's properties, events, and methods. This process controls the publishing and discovery of bean operations and properties.

To obtain information about a specific JavaBean one can use the static getBeanInfo () method of the Introspector class. This method returns an instance of the BeanInfo class, which describes all features a JavaBean exposes. use of this method is shown in the following code fragment: FontSelector fs = new FontSelector (); BeanInfo bi = Introspector . getBeanInfo ( fs . getClass ());

The following example represents code to perform introspection : import java.beans.BeanInfo ; import java.beans.Introspector ; import java.beans.IntrospectionException ; import java.beans.PropertyDescriptor ; public class BeanDemo { private final String name = " BeanDemo "; private int size; public String getName () { return this.name; } public int getSize () { return this.size ; } public void setSize ( int size ) { this.size = size; } public static void main( String[] args ) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo ( BeanDemo.class ); for ( PropertyDescriptor pd : info.getPropertyDescriptors () ) System.out.println ( pd.getName () ); } }

The above example creates a non-visual bean displays the following properties derived from the BeanInfo object. out p ut : class name size

BeanInfo Interface A bean implementor who wishes to provide explicit information about their bean may provide a BeanInfo class that implements this BeanInfo interface and provides explicit information about the methods, properties, events, etc, of their bean.

Method Detail getBeanDescriptor public BeanDescriptor getBeanDescriptor () Gets the beans BeanDescriptor. Returns: A BeanDescriptor providing overall information about the bean

getEventSetDescriptors public EventSetDescriptor [] getEventSetDescriptors () Gets the beans EventSetDescriptors. Returns: An array of EventSetDescriptors describing the kinds of events fired by this bean

getDefaultEventIndex public int getDefaultEventIndex () A bean may have a "default" event that is the event that will mostly commonly be used by humans when using the bean. Returns: Index of default event in the EventSetDescriptor array returned by getEventSetDescriptors.Returns -1 if there is no default event.

getPropertyDescriptors public PropertyDescriptor [] getPropertyDescriptors () Gets the beans PropertyDescriptors. Returns: An array of PropertyDescriptors describing the editable properties supported by this bean.

getDefaultPropertyIndex public int getDefaultPropertyIndex () A bean may have a "default" property that is the property that will mostly commonly be initially chosen for update by human's who are customizing the bean. Returns: Index of default property in the PropertyDescriptor array returned by getPropertyDescriptors.Returns -1 if there is no default property.

getMethodDescriptors public MethodDescriptor [] getMethodDescriptors () Gets the beans MethodDescriptors. Returns: An array of MethodDescriptors describing the externally visible methods supported by this bean

getAdditionalBeanInfo public BeanInfo [] getAdditionalBeanInfo () that provide additional information on the current bean getIcon public Image getIcon (int iconKind) This method returns an image object that can be used to represent the bean in toolboxes

Properties There exist four different kinds of properties a JavaBean can expose: Simple Indexed Bound Constrained All types of property have in common that they are characterized by a pair of set/get methods.

Simple Properties As the name suggests, simple properties are the simplest of the four. we would like to have a default font size, which will be used as initial font size at runtime.

Design patterns, where N is the name of the property and T is its type. public T getN ( ); public void setN (T arg );

The following listing shows a class that has three read/write simple properties: public class Box { private double depth, height, width; public double getDepth( ) { return depth; } public void setDepth(double d) { depth = d; } public double getHeight( ) { return height; } public void setHeight(double h) { height = h; } public double getWidth( ) { return width; } public void setWidth(double w) { width = w; } }

Indexed Properties If a simple property can hold an array of value they are no longer called simple but instead indexed properties An indexed property may expose set/get methods to read/write one element in the array and/or so-called ’array getter/setter’ which read/write the entire array.

design patterns, where N is the name of the property and T is its type: public T getN ( int index); public void setN ( int index, T value); public T[ ] getN ( ); public void setN (T values[ ]);

The following listing shows a class that has one read/write indexed property: public class PieChart { private double data[ ]; public double getData(int index) { return data[index]; } public void setData(int index, double value) { data[index] = value; } public double[ ] getData( ) { return data; } public void setData(double[ ] values) { data = new double[values.length]; System.arraycopy(values, 0, data, 0, values.length); } }

Bound Properties A Bean that has a bound property generates an event when the property is changed. Bound Properties are the properties of a JavaBean that inform its listeners about changes in its values. Bound Properties are implemented using the PropertyChangeSupport class and its methods. Bound Properties are always registered with an external event listener.

The event is of type PropertyChangeEvent and is sent to objects that previously registered an interest in receiving such notifications. In order to provide this notification service a JavaBean needs to have the following two methods: public void addPropertyChangeListener ( PropertyChangeListener p) { changes.addPropertyChangeListener (p); } public void removePropertyChangeListener ( PropertyChangeListener p) { changes.removePropertyChangeListener (p); }

Constrained Properties A Bean that has a constrained property generates an event when an attempt is made to change its value. Constrained Properties are implemented using the PropertyChangeEvent class. The event is sent to objects that previously registered an interest in receiving such notifications. Those other objects have the ability to veto the proposed change. This capability allows a Bean to operate differently according to its run-time environment.

Constrained Properties are the properties that are protected from being changed by other JavaBeans. Constrained Properties are registered with an external event listener that has the ability to either accept or reject the change in the value of a constrained property. Constrained Properties can be retrieved using the get method. The prototype of the get method is: Syntax : public string get< ConstrainedPropertyName >() Can be specified using the set method. The prototype of the set method is: Syntax : Public string set< ConstrainedPropertyName >(String str )throws PropertyVetoException

Persistence Persistence is the ability to save a Bean to nonvolatile storage and retrieve it at a later time. Inorder to do this we take the help of BDK. Saving the beans in the BDK :choose “file” and then the “save” options. Restoring the beans in the BDK :choose “file” and then “load ” options.

The Java Beans API The Java Beans functionality is provided by a set of classes and interfaces in the java.beans package.

Interface Description AppletInitializer Methods in this interface are used to initialize Beans that are also applets. BeanInfo This interface allows a designer to specify information about the properties, events, and methods of a Bean. Customizer This interface allows a designer to provide a graphical user interface through which a Bean may be configured. ExceptionListener A method in this interface is invoked when an exception has occurred PropertyChangeListener A method in this interface is invoked when a bound property is changed. PropertyEditor Objects that implement this interface allow designers to change and display property values

Interface Description VetoableChangeListener A method in this interface is invoked when a constrained property is changed. Visibility Methods in this interface allow a Bean to execute in environments where a graphical user interface is not available.

Class Description Encoder Encodes the state of a set of Beans. Can be used to write this information to a stream EventHandler Supports dynamic event listener creation EventSetDescriptor Instances of this class describe an event that can be generated by a Bean FeatureDescriptor This is the superclass of the PropertyDescriptor , EventSetDescriptor , and MethodDescriptor classes. IndexedPropertyDescriptor Instances of this class describe an indexed property of a Bean. SimpleBeanInfo This class provides functionality that can be used when writing BeanInfo classes

Class description IntrospectionException An exception of this type is generated if a problem occurs when analyzing a Bean. Introspector This class analyzes a Bean and constructs a BeanInfo object that describes the component MethodDescriptor Instances of this class describe a method of a Bean. ParameterDescriptor Instances of this class describe a method parameter. PropertyChangeEvent This event is generated when bound or constrained properties are changed PropertyDescriptor Instances of this class describe a property of a Bean. PropertyEditorManager This class locates a PropertyEditor object for a given type.