UNIT 5 Java Beans Components PDF
UNIT 5 Java Beans Components PDF
1) Introduction to Beans ?
A bean is a reusable software component based on sun’s JavaBeans
specification that can be manipulated in a builder tool.
Java Beans
• can be defined as an independent software component that is
developed using Java. The main usage of Javabeans is its reusability.
• A Java Beans have three fundamental components:
a) Event: - can be defined as any change in the state of a bean or in its
environment.
b) Properties: - defined as private members, data that can be accessed
as & when required
c) Methods: - typically in Javabeans are required to get/access or
set/specify property of a beans and also we may code methods to
perform another operations/tasks.
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
2 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
The simplest kind of bean is really nothing more than a java class that
follows Builder tools use this standard naming conventions for its methods.
Properties are conceptually at a higher level than instance fields –they are
features of the interface, whereas instance fields belong to the
implementation of the class.
Real - world Beans are much more elaborate & tedious to code, because of
two reasons:
2) The same bean must be usable in a wide variety of contexts. Both the
behavior & appearance of your bean is customizable.
This bean gives user a convenient way of entering dates, simply by locating
them in a calendar display.
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
3 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
By using a bean such as this one, u can take advantage of the work of
others, simply by dropping the bean into a builder tool.
• In Java beans program, we may define property & these property can
be edited as & when required.
• To declare property(s), we declare private members data/private
member variables in a Java bean program.
• To edit these properties Java Beans provides two methods: -
1. Accessor Method: - this method enables us to retrieve Java
bean properties. Here getProperty() method is used.
2. Mutator Method: - is used to specify or set a property in Java
bean program, use setProperty().
Bean writing is a process to defining the properties, registering with events
and implementing the methods. It is the construction state of the bean.
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
4 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
To make any bean usable in a builder tool, package into a JAR file all class
files that are used by the bean code.
A JAR file for a bean needs a manifest file that specifies which class files in
the archive are beans & should be included in the ToolBox.
If your bean contains multiple class files, just mention in the manifest
those class files that are beans & that you want to have displayed in the
toolBox.
Ex:
jar cvfm ImageViewerBean.jar ImageViewerbean.mf
com/horstmann/corejava/*.class
You can also add other items such as GIF files for icons,to the JAR file.
Builder Environments have a mechanism for adding new beans, typically
By loading JAR files. Here is what you do to import beans into NetBeans.
Compile the ImageViewerBean & FileNameBean classes & package them
into JAR files. Then start NetBeans & follow these steps :
In the file dialog box, move to the ImageViewerBean directory & select
ImageViewerBean.jar
Now a dialog box pops up that lists all the beans that were founding the
JAR file.Select ImageViewerBean.
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
5 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
Finally, you asked into which palette you want to place the beans. Select
Beans.
In this you will know basic rules for designing your own beans. First we
want to know that there is no cosmic beans class that you extend to build
your beans .Visual beans directly or indirectly extend the Component class.
But non visual beans don’t have to extend any particular superclass.
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
6 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
1) If the bean writer uses standard naming patterns for properties &
events, then the builder tool can use the reflection mechanism to
understand what properties & events the bean is supposed to expose.
2) The bean writer can supply a bean information class that tells the
builder tool about the properties & events of the bean.
If u have a get method but not an associated set method, u define a read-
only property.
There is one Exception to the get/set naming pattern. Properties that have
boolean values should use an is/set naming pattern like:
A bean builder envt. will infer that your bean generates events when
you supply methods to add & remove eventlisteners.
All event class names must end with in Event, & the classes must
extend the
EventObject class.
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
7 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
2) Indexed Properties –
An indexed property is one that gets or sets an array.
A chart bean uses an indexed property for data points.
With an indexed property u supply two pairs of get & set methods: one
for the array & one for the individual entries.
They must follow this pattern:
a) Type[ ] getPropertyName()
b) void setPropertyName(Type [ ] x)
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
8 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
c) Type getPropertyName(int i)
d) void setPropertyName(int i ,Type x)
Bound properties tell interested listeners that their value has changed.
To implement a bound property, u must implement two mechanisms:
a) Whenever the value of the property changes, the bean must send a
PropertyChangeEvent to all registered listeners. This change can
occur when the set method is called or when the program user carries out
an action such as editing text or selecting a file.
Ex:
changeSupport.firePropertyChange(“running”,false,true) ;
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
9 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
4) Constraint properties –
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
10 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
6) BeanInfo Classes –
As your beans become complex, there may be features of your bean that
naming patterns will not reveal. Many beans have get/set method pairs
that should not correspond to bean properties.
Therefore the JavaBeans specification allows a far more flexible & powerful
mechanism for storing information about your bean for use by a builder.
You can define an object that implements the BeanInfo interface to describe
your bean. When you implement this interface, a builder tool will look to
the methods from the BeanInfo interface to tell it about the features that
your bean supports. U need to follow a naming pattern to associate a
BeanInfo object to the bean.The name of the bean Info class must be
formed by adding BeanInfo to the name of the bean.
For Ex: the bean info class associated to the class ImageViewerBean must
be named ImageViewerBeanBeanInfo.
The bean info class must be part of the same package as the bean itself.
The most common reason for supplying a BeanInfo class is to gain control
of the bean properties.
U construct a PropertyDescriptor for each property by supplying the name
of the property & the class of the bean that contains it.
ICON_COLOR_16*32
ICON_COLOR_32*32
ICON_COLOR_16*16
ICON_COLOR_32*32
7) Property Editors –
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
11 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
For Ex: a property editor for a date object might be a calendar that lets the
user scroll through the months & pick a date.
These property editors are registered with the property editor manager.
The process of supplying a new property editor is :
descriptor.setPropertyEditorClass(TitlePositionEditor.class);
Ex: for the chart bean the descriptors for various properties are:
A Color property, graphColor.
A String property, title.
An int property, titlePosition.
A double [] property, values.
PropertyEditorManager.registerEditor( Date.class,
CalendarSelector.class);
a) It looks first to see which property editors are already registered with
it.
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
12 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
b) Then it looks for a class with a name that consists of the name of the
type plus the word editor.
c) If neither looks up succeeds, then findEditor returns null.
Any property editor you write must implement the PropertyEditor interface,
an interface with 12 methods.It is far more convenient to extend the
convenience PropertyEditorSupport class that is supplied with the
standard library. This support class comes with methods to add & remove
property change listeners, and with default versions of all other methods of
the PropertyEditor interface. For example,our editor for editing title position
of a chart in our chart bean starts out like this:
Before writing a property editor, we should point out that the editor is
under
the control of the builder, not the bean. The builder adheres to the
following procedure to display the current value of the property:
1. It instantiates property editors for each property of the bean.
2. It asks the bean to tell it the current value of the property.
3. It then asks the propertyeditor to display the value.
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
13 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
Now, the text field displays one of these fields. When the user edits the
textfield, this triggers a call to the setAsText method to update the
property
value by invoking the setValue method. It is a generic method whose
parameter is of type Object.To set the value of a numeric type, we need
to pass a wrapper object.
public void setAsText(String s)
{
for (int i =0; i < options.length; i++)
{
if (options[i] .equals(s))
{
setValue(i);
return;
}
}
}
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
14 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
The dialog box contains a component to edit the property values, supplied
by the property editor & various buttons, supplied by the builder envt.
1) Tell the builder tool that u will paint the value & not use as a string.
2) “Paint” the value the user enters onto the GUI.
3) Tell the builder tool that u will be using a GUI based property editor.
4) Build the GUI.
5) Write the code to validate what the user tries to enter as the value.
For the first step, you override the getAsText method in the PropertyEditor
interface to return null and the isPaintable method to return true.
public String getAsText()
{
return null;
}
public boolean isPaintable()
{
return true;
}
Then, you implement the paintValue method. It receives a Graphics context
& the coordinates of the rectangle inside which you can paint.
Now, write the code that builds up the component that will hold the custom
editor. For example, associated to our InverseEditor class is an
InverseEditorPanel class that describes a GUI with two radio buttons to
toggIe between normal and inverse mode. However, the GUI actions must
update the property values. We did this as follows:
Customizers: -
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad
15 | Unit – 5 J a v a B e a n s C o m p o n e n t s ( A d v a n c e J a v a )
__________________________________________________________________________
Mohit Chowdhary,Poonam Chaudhary
Assistant Professor,Deptt of Computer Engineering
FET,Manav Rachna International University,Faridabad