Upload
jobishcthayil
View
224
Download
0
Embed Size (px)
Citation preview
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 1/61
Java Beans
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 2/61
Java Beans: Features – Designing Java Beans –
Creating and using properties – Induced -
bound and constrained properties - using and
creating events – Introspection – creating &using Beanlnfo clauses – customization –
providing custom property editors and GUI
interfaces.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 3/61
BEAN is resuable software component model
Can be manipulated visually in a builder tool.
Software component model :describe how to create and
use resuable software components to build anapplication.
Builder tool is an application development tool
(Ex:Jbuilder,VisualAge )
These tools help to create new beans or use existingbeans to create an application.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 4/61
Reusable Components
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 5/61
Beans are reusable software components
written in Java, that you can use in a variety of
programming environments including third
party environment.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 6/61
Reusable components (BEANS)can create using the
tool BDK(Beans Development kit)
Beanbox pgm comes with BDK is used to create an
application. Beanbox consist of 3 windows
1)Toolbox
2)Beanbox
3)Property sheet
If u close any one of the window all 3 will closed
automatically.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 7/61
Toolbox :list of all beans(ex:blue button,
juggler,molecule,event monitor etc)
Beanbox: largest window ,provides an area to layoutand connect the beans selected from toolbox.
Propery sheet :smallest window ,lists the currently
selected bean‟s properties and their values.(ex:background,foreground,name etc)
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 8/61
Bean BoxTool Box Properties
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 9/61
Install Java Development Kit (JDK1.3)and Bean development Kit (BDK1.1)
Running the BeanBox tool .
change to directory c:\bdk\beanbox andexecute the run.bat file.
The Bean Box tool appears in design mode displaying three windows-
the Toolbox, Bean Box and properties Window.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 10/61
: Creating an Application
The application consists of three beans thatare supplied with the beanbox tool-
a juggler bean and two OurButton beans
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 11/61
For adding a juggler bean to the Beanbox window, first
point to the juggler icon in the toolbox and click the left
mouse button. move cursor to the Bean box window.
Newly added bean display a striped boundary, indicatesthat the bean has the focus.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 12/61
Then add 2 OurButton bean and change the Label
property from Press to Start .Given below is the Bean
Box with juggler bean(an animation bean that displays
a little man named Duke)and two OurButton bean.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 13/61
Bean BoxTool Box Properties
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 14/61
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 15/61
Step 4: Connecting Bean
Step 5: Running in Runtime mode
Step 6: Saving and Restoring an Application
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 16/61
MOLECULE BEAN AND OURBUTTON BEAN
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 17/61
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 18/61
From the Event Target Dialog box select the “Rotate on
X” method and “Rotate on Y” method accordingly. Andclick OK button.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 19/61
Property
Events
Introspection
Customization
Persistence
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 20/61
Enables developers to customize and program with
beans
Such as color, size and string to be used as label.
set() and get() methods are the heart of the java beanproperties mechanism.
Also called getter and setter.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 21/61
“get” method used to read values of the propery.
“set” method sets the value of the property.
Property is a subset of Bean‟s state.
It determines the behavior and appearance of that
component
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 22/61
Bean properties are equivalent to the data fields in java
object
except that bean properties must declare as private datafields
and they must be accessible through special public
methods known as accessor methods.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 23/61
Accessor methods are public methods used to get thevalue of a bean‟s property.
All accessor method names start with the get prefix.
Typically the get prefix is followed by the property
name.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 24/61
Example: - A simple accessor method for a back ground property should like the following
Public Color getBackgroundColor(){
Return backgroundColor;
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 25/61
Public methods used to set the value of a Bean‟sproperty.
Starts with the set prefix followed by the property
name.
public void setBackgroundColor(Color color)
{
backgroundColor=color;
repaint();}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 26/61
Mutator methods use the void return value
they take a parameter of the same data type as
the property they set.
When a property changes, the bean should reflect thatchange.
So the bean is repainted.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 27/61
When an object’s a Bean?
◦
A simple java object becomes a bean when all of the object‟s data fields are private and are accessible through methods
known as accessor methods.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 28/61
Enables Beans to communicate and connect together.
Ability of Bean to send event notification to other bean,applications, or scripting languages when internal
property changes.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 29/61
Introspection
Enables a builder tool to analyze how a bean works .
A mechanism that allows classes to publish the
operations and properties they support
Customization: - to customize the appearance or
behavior of the bean.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 30/61
Persistence: - enables developers
to customize beans in an application builder
Retrieve those beans with customized features
Persistence means: Bean properties should remain thesame, until they are explicitly changed through a
customization mechanism.
For example, suppose we change the OurButton
bean‟s(bean created by the user) Label property from„ Press’ to ‘Start’ , its value remain same as Start unlessyou decide to change it again.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 31/61
Simple
Indexed
Bound
Constrained
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 32/61
Simple◦ that takes a single value such as string or number
Indexed
◦ has an array value and for which the bean provides methods to
set and get individual elements as well as methods to get and
set the entire array.
◦ There are four design patterns
Indexed Accessor Methods
Indexed Mutator Methods Getting Whole Arrays
Setting Whole Arrays
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 33/61
Indexed Accessor Methods
just like single-valued property accessor methods,
except they have an integer index parameter.
public PropertyType getPropertyName (int position) Example: -
public double getValues(int i)
{
return values[i];
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 34/61
Indexed Mutator Methods
are written just like the single-valued property mutator
method, except that have an integer index parameter.
Example : -
public void setValues(int i, double value)
{
values[i] = value;}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 35/61
Getting Whole Arrays - Accessor method
This type of accessor method is just like single-valued
property accessor method, except that it returns the
indexed properties array data type.
Example:
public double[] getValues()
{
return values;}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 36/61
Setting Whole Arrays - Mutator method
This method is for setting a whole indexed property
array which is just like the single- valued property
mutator methods except that its parameter is the
indexed property‟s array data type.
Example: -
public void setValues(double[] v)
{values = v;
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 37/61
Bound◦ A bound property is a property that fires an event when its
value changes.
◦ Beans signal property changes using the
java.beans.PropertyChangeEvent class.◦ java.beans.PropertyChangeListener is the listener Interface
for receiving changed beans bound property events.
Example: - Filename property in a FileNameBean isa bound property. When the filename changes the
bean automatically notified and it loads a new file.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 38/61
Mechanism for implementing bound property◦ Whenever the value of a property changes, the bean
must send a to all registered listeners.
◦ To enable interested listeners to register themselvesthe bean has to implement the following twomethods.
void addPropertyChangeListener(PropertyChangeListener listener)
void removePropertyChangeListener(PropertyChangeListener listener)
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 39/61
java.beans package has a convenient class called
listeners for you.
To use this class your bean must have a
datafield of this like this:
private PropertyChangeSupport change=new PropertyChangeSupport(this)
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 40/61
public void addPropertyChangeListener(PropertyChangeListener listener){
change.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener)
{
change.removePropertyChangeListener(listener);
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 41/61
Finally, when the change happens (setSalary in this case),you check to see if the property value changed, and if
so, notify all the listeners.
public void setSalary (float salary) {
Float oldSalary = new Float (this.salary);
this.salary = salary;
changes.firePropertyChange (
"salary", oldSalary, new Float (this.salary));
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 42/61
Constrained ◦ A constrained property is a property that fires an event when a new
value is being applied to the property that does not fall within
specified constraints.
There is a convenient class called VetoableChangeSupport that manage the Vetoable change listeners for you.
To use this class your bean must have an object of this
class.
private VetoableChangeSupport vetosupport=new
VetoableChangeSupport (this)
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 43/61
Java.beans.VetoableChangeListener is the listener interface forreceiving changed beans constrained property.
public void addVetoableChangeListener(VetoableChangeListener listener)
public void removeVetoableChangeListener(VetoableChangeListener listener)
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 44/61
public void addVetoableChangeListener(VetoableChangeListener listener){
vetosupport.addVetoableChangeListener(listener);
}
public void removeVetoable ChangeListener(VetoableChangeListener
listener){
change.remove VetoableChangeListener(listener);
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 45/61
The various steps involved are:◦ Creating a simple Bean
◦ Compiling and saving the Bean into a Java Archive(JAR) file
◦Loading the Bean into the ToolBox
◦ Dropping a Bean instance into the BeanBox
◦ Inspecting the Bean's properties, methods, andevents
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 46/61
Step1: Write the HelloBean code
◦ Put it in a file named Hello.java, in the directory of your
choice. Suppose the folder created is in C:\btest.
◦ The Hello Bean that defines a rectangle of a certain size in
its constructor. Within this rectangle, the Bean draws the
string „Hello‟ in black.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 47/61
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
public class Hello extends Canvas
{ public Hello()
{setSize(75,40);
}
public void paint(Graphics g)
{
Dimension dimension=getSize();
g.drawString("Hello",dimension.width/3,dimension.height/2);
}
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 48/61
Step 2 :Compile the Bean using Javac Hello.java andit produces the class file Hello.class
Step 3: Create a manifest file. Use your favorite text
editor to create a file, we'll call it manifest.tmp, that
contains the following text:Name: Hello.class
Java-Bean: True
The manifest file specifies the name of the class file and
indicates that it is a JavaBean. The manifest file becomes partof the JAR file.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 49/61
Step 4: Create the executable JAR file. Use the form of the jarcommand to include the manifest file along with the
Hello.class file (Type the command on one line):
◦ jar cfm Hello.jar manifest.tmp Hello.class
Step 5: Load the JAR file
Step 6: Drop a Hello Bean instance into the Bean Box.
Step 7 Inspecting Hello Bean Properties and Events
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 50/61
Create the directory C:\counter andstore the java source file in this directory
Write the Java Source file and compileit
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 51/61
//counter.javaimport java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class counter extends Canvas
{
int count;
public counter()
{ addMouseListener(new MouseAdapter()
{ public void mousepressed(MouseEvent e)
{clicked();}
};
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 52/61
count=0;setSize(200,100);
}
public void clicked()
{
count++;
repaint();
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 53/61
public void paint(Graphics g){
Dimension dimension=getSize();
int height=dimension.height;int width=dimension.width;
g.setColor(new Color(255,0,0));
g.fillRect(0,0,--width,--height);
g.setColor(new Color(0,0,0));
g.drawString("count="+count,50,50);
}
}
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 54/61
Step 3 : Create the Manifest file counter.mft with thefollowing content.
Name: counter.class
Java-Bean: True
Step 4:Pack the Bean code into jar file using the
following command
jar cfm Counter.jar counter.mft *.class
Step 5: Load the JAR file
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 55/61
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 56/61
BeanInfo interface allows a designer to specifyinformation about the properties, events and methods
of a bean.
This interface defines several methods, including
◦ PropertyDescriptor[] getPropertyDescriptors()
◦ EventSetDescriptor[] getEventSetDescriptors()
◦ MethodDescriptors[] getMethodDescriptors()
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 57/61
Following are some of the classes defined in Java.beans package.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 58/61
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 59/61
SimpleBeanInfo is a class that provides default implementations of the BeanInfo Interface, including
the three methods just shown.
The Java Bean specification required to use a naming
pattern to associate a BeanInfo object to the bean.
Specify the name of the bean into the class by adding BeanInfo to the name of the bean.
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 60/61
For Example our ColorsBeanInfo class starts like this:Public class ColorsBeanInfo extends SimpleBeanInfo
{
Public EventSetDescriptor[] getEventSetDescriptors()
{
…………..
}
…………….. }
7/31/2019 Wt Module III Javabean
http://slidepdf.com/reader/full/wt-module-iii-javabean 61/61
that is used isshown below:
PropertyDescriptor(String property ,class beanclass) throws IntrospectionException