JAVA BEANS

advertisement
JAVA BEANS
• Java Bean is a software component that has been
designed to be reusable in a variety of different
environments
• There is no restriction on the capability of a bean.
• It may perform a simple function such as checking
the spelling of a document (or) A complex function
such as forecasting the performance of a stock
portfolio.
• A bean may be visible to an end-user .
• One example of this is a button on a graphical user
interface (GUI).
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• A bean may also be invisible to a user .
• EX:Software to decode a stream of multimedia
information in real time.
• Finally a bean may be designed to work
autonomously on a user’s workstation or to work in
co-operation with a set of other distributed
components .
• Note: some specific changes a s/w developer must
make to a class so that it is usable as a java bean.
One of the goal of java designer is to make it easy to
use this technology. Therefore, code changes must
be minimal.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Advantages of java beans:
• A bean obtains all the benefits of java’s “writeonce; run-anywhere” paradigm.
• The properties, events and methods of a bean
that are exposed to an application builder can
be controlled.
• A bean may be designed to operate correctly
in different locals, which makes it useful in
global markets.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
(contd..)
• Auxiliary software can be provided to help a person
configure a bean. This software is needed when the
design time parameters for that component are
being set. It doesn’t need to be included in the
runtime environment.
• The configuration settings of a bean can be saved in
persistent storage and restored at a later time.
• A bean may registered to receive events from other
objects and can generate events that are sent to
other objects.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Application builder tools:
• When working with java beans ,most developers use
an application builder tool ,a utility that enables you
to configure a set of beans ,connect them together
and produce a working application.
• Bean builder tools have the following capabilities:
• A palette is provided that lists all of the available
beans. As additional beans are developed or
purchased, they can be added to the palette.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• A worksheet is displayed that allows the designer to
layout beans in a GUI. A designer may drag and drop
a bean from the palette to this worksheet.
• Special editors and customizers allow a bean to be
configured. This is the mechanism by which the
behavior of a bean may be adapted for a particular
environment.
• Commands allow a designer to inquire about the
state and behavior of a bean. This information
automatically becomes available when bean is added
to the palette.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• Capabilities exists to interconnect beans. This
means that events generated by one
component are mapped to method
invocations on other components.
• When a collection of beans has been
configured and connected, it is possible to
save all of this information in a persistent
storage area. At a later time, this information
can then be used to restore the state of the
application.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• Sun provides two bean application builder
tools.
Bean-box ( part of BDK)
Bean builder.
• Using Bean-developer kit (BDK):
BDK available from the java soft site, is a
simple example of a tool that enables to
create, configure and connect set of beans.
Sreenivasa Institute of Technology and
Management Studies, Chittoor

Installing the BDK:
The java2SDK must be installed on your
machine for the BDK to work. Confirm that
the SDK tools are accessible from your
environment. BDK can be downloaded from
http://java.sun.com. Follow the instructions
and install it in C:/BDK.
• Starting the BDK:
To start bean, follow 3 steps:
1. Change to the directory c:/BDK/beanbox.
2. Execute the batch file called run.bat.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
This causes the BDK to display windows.
i. Tool box
ii. Bean box
iii. Properties
• Tool box, lists all of the different beans that
have been included with the BDK.
• Bean box, provides an area to layout and
connect the beans selected from the tool
box.
• Properties provides the abilities to configure
a selected bean.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
•
•
•
•
Using BDK
Tool box contains beans like my button, ticktock bean, molecule bean, color bean etc.,
A molecule bean displays a 3-D view of a
molecule.
It may be configured to present one of the
molecules like Benzene, cyclo-hexane , ethane
or water etc.,
This component has two methods: rotateX,
rotateY.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Create and configure an instance of the Molecule
bean:
• Position the cursor on the tool box entry labeled
molecule and click the left mouse button. You should
see cursor changes to a cross.
• Move the cursor to the bean box display area and
click the left mouse button appropriate area where
you wish the bean to be displayed.
• You should see a rectangular region appear that
contains a 3-D display of a molecule. This area is
surrounded by a hatched border, indicating selected.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• 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. Notice that the bean display changes
immediately when you change the selected
molecule.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Create an instance of the ticktock bean and
connect to molecule bean:
• Position the cursor on the tool box entry
labeled molecule and click the left mouse
button.
• Move the cursor to the bean box display area
and click the left mouse button in appropriate
area where you wish the bean to be displayed.
• Repositioning of ticktack bean can be done by
dragging the hatched border.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• Go to the menu bar of the bean box and select
edit /events/propertychange. Observe a line
extending from the button to the cursor.
• Move the cursor so that it is inside the
molecule bean display area and click the left
mouse button. You should see the event
target dialog dialog box.
• The dialog box allows you to choose a method
that should be invoked when event is
generated periodically by ticktack bean.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• Select the entry labeled “rotateX” and click
the OK button. You should see a message box
appear very briefly stating that the tool is
“Generating and compiling adaptor class”.
• Each time you press the button, the molecule
should move few degrees around one of its
axes.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Jar files
• BDK expect beans to be packaged within JAR file.
• A JAR file allows to efficiently deploy a set of classes
and their associated resources.
• For ex, a developer may build a multimedia
application that uses various sound and image files.
• A set of beans can control how and when this
information is presented.
• All these pieces can be placed into one JAR file.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Advantages of JAR:
• Makes easy to deliver and install s/w.
• Elements in a JAR file are compressed, which
makes downloading faster.
• Digital signatures may also be associated with
the individual elements in a JAR file.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Manifest files:
• A developer must provide a manifest file to indicate which of
the components in a JAR file are Java beans.
• Ex:-Contains four .gif files and one class file.
Manifest-Version: 1.0
Name: sunw/demo/slides/slide0.gif
Name: sunw/demo/slides/slide1.gif
Name: sunw/demo/slides/slide2.gif
Name: sunw/demo/slides/slides.class
Java-Bean: True
(having extension .mft)
• A manifest file may reference several .class files. If a .class file
is a java bean ,its entry must be immediately followed by the
line
“Java-Bean: True”.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
The JAR utility
A utility used to generate a JAR file.
Syntax: jar options files.
Creating a JAR file:
Syntax: jar cf xyz.jar *.class *.gif
(create a jar file xyz.jar with all .class and .gif
files from the current directory)
• If a manifest file such as xyz.mf is available,
the command is
jar cfm xyz.jar xyz.mf *.class *.gif.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• Tabulating the contents of jar file:
jar tf xyz.jar;
• Extracting files from a jar file:
jar xf xyz.jar;
• Updating an existing jar file:
jar –uf xyz.jar file1.class;
(adds the file “file1.class “ to an existing jar “xyz.jar”.)
• Recursing directories:
jar –uf xyz.jar –c directoryX
(adds all files of directoryX to xyz.jar)
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Introspection
• It is the process of analyzing a bean to
determine its capabilities.
• This is an essential feature of the java beans
API, because it allows an application builder
tool to present information about a
component to a s/w designer.
• Without introspection, the java beans
technology could not operate.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
•
There are two ways in which the developer
of a bean can indicate which of its
properties, events and methods should be
exposed by an application builder tool.
1. Simple naming conversions are used. (Allow
introspection mechanism to enter
information about a bean.)
2. An additional class is provided that explicitly
supplies this information.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Design patterns for properties:
• A property is a subset of a bean’s state.
• The values assigned to the properties
determine the behavior and appearance of
that component.
• There are three properties
1. Simple
2. Boolean
3. Indexed
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Simple property:
• A simple property has a single value.
• It can be identified by the following design
patterns, where N is the name of the property
and T is its type.
public T getN();
public void setN(T arg);
• A read/write property has both of these
methods access to its values.
• A read-only property has only a get method.
• A write-only property has only a set method.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Ex:public class Box
{
private double depth, height, width; //properties
public double getDepth() //design pattern
{
return depth;
}
public void setDepth(double d)
{
depth=d;
}
(contd..)
Sreenivasa Institute of Technology and
Management Studies, Chittoor
public double getHeight()
{
return height;
}
public void setHeight( double h)
{
height=h;
}
public double getWidth()
{
return width;
}
public void setWidth( double w)
{
width=w;
}
}
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Boolean properties:
• A boolean property has a value of true or false.
• It can be identified by the following design
patterns, where N is the name of the property.
public boolean isN();
public boolean getN();
public void setN(boolean value);
• Either first/second pattern can be used to retrieve
the value of a Boolean property.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Ex:public class Line
{
private boolean dotted=false;
public boolean isDotted()
{
return dotted;
}
public void setDotted ( boolean dotted)
{
this. dotted= dotted;
}
}
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Indexed properties:
• An indexed property consists of multiple
values.
• It can be identified by the following 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[ ]);
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Ex:public class Piechart
{
private double data[];
public double getData(int index)
{
return data[index];
}
Public void setData(int index, double value)
{
data[index]=value;
}
(contd..)
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Public double[ ] getData()
{
return data;
}
Public void setData(double[] values)
{
data= new double[values.length];
System.arraycopy ( values, 0,data , 0,
values.length);
}
}
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Developing a simple Bean using the BDK:1. Create a directory for the new Bean.
2. Create the java source file(s).
3. Compile the source file(s).
4. Create a manifest file.
5. Generate a JAR file.
(ex:- jar –cfm sitams.jar sitams.mft sitams.class)
6. Copy jar file to jars directory in BDK and Start
the BDK.
7. Test
Sreenivasa Institute of Technology and
Management Studies, Chittoor
1)
2)
Create a directory for the new Bean and change to it. Ex:C:\BDK\demo\sunw\demo\colors.
Create the source file for the Bean.
Note:
After creating and deploying a bean, to display output on the display
area, use paint() in the bean program.
Ex:public void paint(Graphics g)
{
double area=(height* width*depth);
String msg=“the area of the box is “ + area;
g.drawString(msg,10,10);
}
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Design patterns for events:
• Beans can generate events and send them to other objects.
• Event – is an activity that interrupts the current ongoing
activity ex:- key press or mouse click etc..
• When user initiates an event, the system creates an event and
passes it to event handling code within the program. This
code is responsible for giving appropriate response.
• These can be identified by the following design pattern,
where T is the type of the event.
public void addTListener( TListener
eventListener);
public void addTListener( TListener eventListener) throws
ToomanyListeners;
public void removeTListener(TListener
eventListener);
Sreenivasa Institute of Technology and
Management Studies, Chittoor
•
Sample Program
package event.EventDemo;
import java.awt.*;
import java.awt.event.*;
public class EventDemo extends Canvas
{
public String msg;
public int x,y;
public EventDemo()
{
setSize(120,120);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent m)
{x=m.getX();
y=m.getY();
msg=" "+x+","+y;
repaint();
}
public void mouseReleased(MouseEvent m)
{
x=m.getX();
y=m.getY();
msg=" "+x+","+y;
repaint();
}
Sreenivasa Institute of Technology and
Management Studies, Chittoor
});
msg=" ";
x=0;y=0;
}
public void paint(Graphics g)
{
g.drawRect(0,0,100,100);
if(x<=100 && y<=100)
g.drawString(msg,x,y);
else
g.drawString("out of boundary",20,20);
}
}
Sreenivasa Institute of Technology and
Management Studies, Chittoor
These methods are used by event listeners to pattern register an
event of specific interest.
1. Indicates that a bean can multicast an event to multiple
listeners.
2. Indicates that a bean can unicast an event to only one
listener.
3. When listener does not require a specific type of event
notification from a bean.
4. JavaBeans events are signalled when an activity occurs,
such as a button being pressed or a window being closed.
The visual editor for Java shows events in the Java Beans
view, and you can use this view to add and remove events.
5. The list of defined events for a JavaBean is described in its
BeanInfo class, which also controls the commonly used or
preferred events.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• You might want to add an event to a JavaBean if you want
something to happen when the JavaBean is generated, such
as a database update when a button is pushed.
• The Java Bean raising the event is the source, and the object
that gets called back when the event is raised is known as the
listener.
• Each JavaBean has an interface that allows it to notify the
listeners for each event, as well as methods to add and
remove listeners.
• Typically, if the source JavaBean has a method XXX, there is a
listener interface, XXXListener, and two methods. It is
important that XXXListener extends java.util.EventListener. If it
does not, and unless a specific BeanInfo is supplied, the event
will not be discovered.
• addXXXListener(XXXListener aListener)
• removeXXXListener(XXXListener aListener)
Sreenivasa Institute of Technology and
Management Studies, Chittoor
• An example of an event is the JavaBean
java.awt.Component, which raises events when the
mouse moves over it.
• The listener interface,
java.awt.event.MouseMotionListener, implements
the following two methods:
• void mouseDragged(MouseEvent evt);
• void mouseMoved(MouseEvent evt);
• To add a mouse listener, the java.awt.Component has
the following two methods:
• public void addMouseListener(MouseListener evt);
• public void removeMouseListener(MouseListener
listener);
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Ex:public class Thermometer
{
public void addTemparatureListener(
temperatureListener t1)
{
--}
public void removeTemparatureListener(
temperatureListener t1)
{
--}
}
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Using Bound Properties:• A bean that has a bound property generates
an event when the property is changed.
• The event is of type propertychange event
and is sent to objects that previously
registered an interest in receiving such
notification.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
public class Currency_converter extends Canvas
{
private String rupees;
private double ex_rate=0.017;
private NumberFormat fmt=NumberFormat.getInstatnce();
public Currency_converter()
{
setSize(70,70);
rupee="1.0";
change();
}
public String getRupees()
{
return rupees;
}
public void setRupees(String rupees)
{
this.rupees=rupees;
Sreenivasa Institute of Technology and
}
Management Studies, Chittoor
public void change()
{
rupees=get_val();
repaint();
}
private String get_val()
{
double value=Double.parseDouble(rupees);
value *=ex_rate;
fmt.setMaximumFractionDigits(3);
fmt.setMinimumFractionDigits(3);
rupees="$"+fmt.format(value);
return rupees;
}
public void paint(graphics g)
{
g.drawString(rupees,10,10);
}
}
Sreenivasa Institute of Technology and
Management Studies, Chittoor
TickTock Bean:• It is supplied with BDK.
• It generates a property change event every N
seconds.
• N is a property of the bean that can be
changed via the properties window of the
BDK.
• Ex:- Build an application that uses the TickTock
Bean to automatically control the colors Bean.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Steps:
1. Create an instance of colors bean and TickTock
Bean.
2. Select TickTock Bean, go to edit/events/
propertychange/ propertychange. A line extends
from the button to the cursor.
3. Drop the cursor inside the colors bean. Display area
and click left mouse button. You should see Event
Target Dialog dialog box.
4. The dialog box allows you to choose a method that
should be invoked when this event occurs.
5. Select labeled “ change” and click Ok button. You
should see a message box appear very briefly,
stating that the tool is “Generating and compiling
adaptor class”.
(note: color of your component should be changing
every second)
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Constrained Properties:• A Bean that has a constrained property
generates an event when an attempt is made
to change its values.
• The event is of type propertychange event.
• The object that is registered will receive
notification.
• This capability allows a bean to operate
differently according to its runtime
environment.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Persistence:
• It is the ability to save a bean to non -volatile
storage and retrieve it at a later time.
• The information that is particularly important are
the configuration settings.
• To save, the colors and Tick tock bean application:
1. Go to menu bar of bean box. Select file/save.
Specify the name of a file to which the beans and
their configuration parameters should be saved.
Click Ok and exit.
2. Start BDK again. Go to menu bar of the bean box,
select file/load and specify the name of the file and
click Ok.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Customizers:
• The properties window of the BDK allows a
developer to modify the properties of a bean.
• However, this may not be the best user interface for
a complex component with many inter-related
properties.
• Therefore, a bean developer can provide a
customizer that helps another developer to configure
this s/w.
• It provides a step by step guide through the process
that must be followed.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Java Beans API:
• Interfaces in java beans package
1. Applet Initializer
2. Bean Info
3. Customizer
4. Exception Listener
5. Property Change Listener
6. Property Editor
7. Visibility.
Sreenivasa Institute of Technology and
Management Studies, Chittoor
Download