wt module iii javabean

61
Java Beans

Upload: jobishcthayil

Post on 05-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 1/61

Java Beans

Page 2: Wt Module III Javabean

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.

Page 3: Wt Module III Javabean

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.

Page 4: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 4/61

Reusable Components

Page 5: Wt Module III Javabean

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.

Page 6: Wt Module III Javabean

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.

Page 7: Wt Module III Javabean

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)

Page 8: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 8/61

Bean BoxTool Box Properties

Page 9: Wt Module III Javabean

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. 

Page 10: Wt Module III Javabean

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 

Page 11: Wt Module III Javabean

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.

Page 12: Wt Module III Javabean

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.

Page 13: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 13/61

Bean BoxTool Box Properties

Page 14: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 14/61

 

Page 15: Wt Module III Javabean

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

Page 16: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 16/61

MOLECULE BEAN AND OURBUTTON BEAN

Page 17: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 17/61

 

Page 18: Wt Module III Javabean

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.

Page 19: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 19/61

Property

Events

Introspection

Customization

Persistence

Page 20: Wt Module III Javabean

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.

Page 21: Wt Module III Javabean

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

Page 22: Wt Module III Javabean

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.

Page 23: Wt Module III Javabean

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.

Page 24: Wt Module III Javabean

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;

}

Page 25: Wt Module III Javabean

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();}

Page 26: Wt Module III Javabean

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.

Page 27: Wt Module III Javabean

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. 

Page 28: Wt Module III Javabean

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.

Page 29: Wt Module III Javabean

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.

Page 30: Wt Module III Javabean

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. 

Page 31: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 31/61

  Simple

Indexed

Bound

Constrained

Page 32: Wt Module III Javabean

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 

Page 33: Wt Module III Javabean

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];

 } 

Page 34: Wt Module III Javabean

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;} 

Page 35: Wt Module III Javabean

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;}

Page 36: Wt Module III Javabean

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;

Page 37: Wt Module III Javabean

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.

Page 38: Wt Module III Javabean

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) 

Page 39: Wt Module III Javabean

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) 

Page 40: Wt Module III Javabean

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);

}

Page 41: Wt Module III Javabean

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));

}

Page 42: Wt Module III Javabean

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)

Page 43: Wt Module III Javabean

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)

Page 44: Wt Module III Javabean

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);

}

Page 45: Wt Module III Javabean

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

Page 46: Wt Module III Javabean

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.

Page 47: Wt Module III Javabean

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);

}

}

Page 48: Wt Module III Javabean

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.

Page 49: Wt Module III Javabean

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

Page 50: Wt Module III Javabean

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

Page 51: Wt Module III Javabean

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();}

};

Page 52: Wt Module III Javabean

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();

}

Page 53: Wt Module III Javabean

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);

}

}

Page 54: Wt Module III Javabean

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

Page 55: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 55/61

Page 56: Wt Module III Javabean

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() 

Page 57: Wt Module III Javabean

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.

Page 58: Wt Module III Javabean

7/31/2019 Wt Module III Javabean

http://slidepdf.com/reader/full/wt-module-iii-javabean 58/61

 

Page 59: Wt Module III Javabean

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. 

Page 60: Wt Module III Javabean

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()

{

………….. 

}

…………….. }

Page 61: Wt Module III Javabean

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