1. 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.
2. A Java Bean is an ordinary java class that confirms the
following rules
1. It provides a default, no-argument constructor.
2. It should be serializable and implement the
Serializable interface.
3. It may have a number of properties which can be
read or written.
4. It may have a number of "getter" and "setter"
methods for accessing properties.
3. Advantages of Java Beans
1. Beans is platform independent, that means it can
be run anywhere.
2. Beans can work in different local platforms.
3. Methods, properties and events of Beans can be
controlled.
4. It is easy to configure Java beans
5. A bean can both receive and create events
6. Configuration settings of a bean can be stored
persistently and can be retrieved any time
4. 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; }
}
5. 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
6. Starting the BeanBox
When you start the BeanBox, you'll see three
windows:
– ToolBox window
– BeanBox window
– Properties window
7.
8. • 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.
9. Create and Configure an Instance of
the Molecule Bean
• Follow these steps
1. Position the cursor on the ToolBox entry labeled
Molecule and click the left mouse button.
2. Move the cursor to the BeanBox display area and
click the left mouse button.
3. You can reposition the Molecule Bean by
positioning the cursor over one of the hatched
borders and dragging the Bean.
4. You can change the molecule that is displayed by
changing the selection in the Properties window
10.
11. 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.
12. • 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 ());
13. 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() );
• }
• }
14. • The above example creates a non-visual bean
displays the following properties derived from
the BeanInfo object.
out put:
class
name
size
15. 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.
16. Method Detail
• getBeanDescriptor
public BeanDescriptor getBeanDescriptor()
Gets the beans BeanDescriptor.
Returns:A BeanDescriptor providing overall
information about the bean
18. • 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.
19. • getPropertyDescriptors
• public PropertyDescriptor[] getPropertyDescriptors()
Gets the beans PropertyDescriptors.
Returns:An array of PropertyDescriptors
describing the editable properties supported
by this bean.
20. • 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.
21. • getMethodDescriptors
• public MethodDescriptor[] getMethodDescriptors()
• Gets the beans MethodDescriptors.
• Returns:An array of MethodDescriptors
describing the externally visible methods
supported by this bean
22. • 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
23. Properties
• There exist four different kinds of properties a
JavaBean can expose:
1. Simple
2. Indexed
3. Bound
4. Constrained
• All types of property have in common that
they are characterized by a pair of set/get
methods.
24. 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.
25. • Design patterns, where N is the name of the
property and T is its type.
public T getN( );
public void setN(T arg);
26. 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;
• }
• }
27. 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.
28. • 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[ ]);
29. • 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);
• }
• }
30. 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.
31. • 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);
}
32. 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.
33. • 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
34. 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.
35. The Java Beans API
• The Java Beans functionality is provided by a
set of classes and interfaces in the
java.beans package.
36. 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
37. 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.
38. 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
39. 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 agiven type.