Sie sind auf Seite 1von 28

Bean Concepts

Why do we need Bean


Hardware Components can be reused i.e., if you
consider ICs which provides advanced
functionality to construct a system, it can be
reused.
It is not necessary or possible to rebuild the
capabilities of IC each time a new system is
needed. With the behavior of IC it can be reused
Similarly a software component can be reused.
How a software component can be
reused
Once the designer will design a software
component.
It can be reused by designer whenever
they select the component.
This is the purpose of bean.
Definition

Java Bean is a Software Component that


has been designed to be reusable in a
variety of different environments.
No restrictions in capability of bean
Benefits of Bean
Write-once, run-anywhere.
Properties of the bean can be controlled
by builder tool.
Configuration of the bean can be stored in
the persistent (non-volatile) storage and
restored.
Bean Builder Tool
There 2 types of Bean Builder tool
available
BDK (Bean development kit) for Java version
before1.4
Bean Builder for Java version 1.4 and later versions
BDK (Bean Developer Kit)
BDK is a tool that enables to create,
configure and connect a set of beans.
There exist a sample set of beans with
their source code
Along with that we can create our bean
Using BDK
Ifu install and run BDK, it displays three
window
Toolbox (Consists of already created bean)
Bean Box (provides an area to layout and
connect beans selected from toolbox)
Properties Box (provides the ability to configure
a selected bean)
Sample Bean available in BDK Toolbox

Blue Button
Explicit Button
Our Button
Event Monitor
Jelly Bean
Molecule

Etc
How to get an instance of already
available bean
Position the cursor on the toolbox entry
labeled with the necessary bean.
Click and drag to the bean box.
It will be placed with hatched border
Can reposition the bean wherever we
want
Can change the value using properties
window.
Introspection of Bean
Introspectionis the process of analyzing a
bean to determine its capabilities.
Bean indicates which of its properties,
events and methods should be exposed
by an application builder
How to design our own properties of our
bean
Design patterns for properties
A property is a subset of a beans state.
The value assigned to the properties
determine the behavior and appearance
of that component
3 types of properties
Simple properties
Boolean properties

Indexed properties
Simple Properties
A simple property has a single value
Public T getN();
Public void setN(T arg);

N name of the property


T- type of the property

EX:
public double getDepth()
public void setDepth(double d)
Boolean Properties
A boolean property has a value of true or
false
public boolean isN();
public boolean getN();
public booean setN(boolean value);
N-name of the property
Indexed Property
Anindexed property consist of multiple
values
public T getN(int index);
public void setN(int index, T value);
public T[] getN();
public void setN(T values[]);
Events in Bean Box
ActionEvent (generated when a button
is presses, a list item is double clicked
or a menu is selected)
AdjustmentEvent (generated when a
scroll bar is manipulated)
ComponentEvent (generated when a
component is hidden, moved, resized)
ContainerEvent (generated when a
component is added or removed from a
container)
Events in Bean box
FocusEvent (generated when a
component gains or loses keyboard
focus)
InputEvent (Abstract super class for all
component input event classes)
ItemEvent (generated when a checkbox
or list item is selected)
KeyEvent (generated when input is
received from the keyboard)
Events in Bean box
MouseEvent (generated when the mouse
is dragged,moved,clicked,pressed or
released)
MouseWheelEvent (generated when the
mouse wheel is moved)
TextEvent (generated when the value of
text area or text field is changed)
WindowEvent ( generated when a
window is activated , closed,
deactivated etc.
Design Patterns for Events
Allthe events specified above are supported by
beans
public void addTListener(TListener eventListener);
public void addTListener(TListener eventListener)
throws TooManyListeners;
public void removeTListener(TListener eventListener);
T type of the event.
EX:
public void addActionListener(ActionListener eventListener)
public void addMouseListener(MouseListener
eventListener)
Developing a simple bean using BDK

Lets create a new bean named as Colors which


is a rectangle or ellipse that is filled with a color.
Steps in creating a bean
Create a directory for the new bean
Create the java source file

Compile the source file

Create a manifest file

Generate a jar file

Start BDK

Test
Creating a directory for the new bean

c:\bdk\demo\sunw\demo\mybean
Create the source file for the new bean
public class MyBean implements java.io.Serializable
{
protected int theValue;
public MyBean()
{
}
public void setMyValue(int newValue)
{
theValue = newValue;
}
public int getMyValue()
{
return theValue;
}
}
Compile the source file
Javac mybean.java
Create a manifest file(mybean.mft)
Name:sunw/demo/mybean/mybean.class

Java-Bean:true
Generate a JAR file
Jar
cfm mybeans.jar
sun/demo/mybean/mybean.mft
Start the BDK
The toolbox window in bdk will include an
entry labeled Mybean for your new bean
Bean
Customization
When you are using a visual development tool to assemble components
into applications, you will be presented with some sort of user
interface for customizing Bean attributes.
These attributes may affect the way the Bean operates or the way it
looks on the screen.
The application tool you use will be able to determine the properties
that a Bean supports and build a property sheet dynamically.
This property sheet will contain editors for each of the properties
supported by the Bean, which you can use to customize the Bean to
your liking.
The Beans class library comes with a number of property editors for
common types such as float, boolean, and String.
If you are using custom classes for properties, you will have to create
custom property editors to associate with them.
In some cases the default property sheet that is created by the
development tool will not be good enough.
You may be working with a Bean that is just too complex to customize
easily using the default sheet.
Beans developers have the option of creating a customizer that can
help the user to customize an instance of their Bean. You can even
create smart wizards that guide the user through the customization
process.
Customizers are also kept separate from the Bean class so that it is not
a burden to the Bean when it
is not being customized. This idea of separation is a common theme in
the JavaBeans architecture.
A Bean class only has to implement the functionality it was designed
for; all other supporting features are implemented separately.
Bean Persistence

It is necessary that Beans support a large variety of storage


mechanisms. This way, Beans can participate in the largest number
of applications.
The simplest way to support persistence is to take advantage of
Java Object Serialization. This is an automatic mechanism for
saving and restoring the state of an object.
Java Object Serialization is the best way to make sure that your
Beans are fully portable, because you take advantage of a standard
feature supported by the core Java platform.
This, however, is not always desirable. There may be cases where
you want your Bean to use other file formats or mechanisms to save
and restore state.
In the future, JavaBeans will support an alternative externalization
mechanism that will allow the Bean to have complete control of its
persistence mechanism.

Das könnte Ihnen auch gefallen