You are on page 1of 61

Java Beans

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.

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 an application. Builder tool is an application development tool (Ex:Jbuilder,VisualAge ) These tools help to create new beans or use existing beans to create an application.

Reusable Components

Beans are reusable software components written in Java, that you can use in a variety of programming environments including third party environment.

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.

Toolbox :list of all beans(ex:blue button, juggler,molecule,event monitor etc) Beanbox: largest window ,provides an area to layout and connect the beans selected from toolbox.
Propery sheet :smallest window ,lists the currently selected beans properties and their values.(ex:background,foreground,name etc)

Tool Box

Bean Box

Properties

Step 1: Install Java Development Kit (JDK1.3) and Bean development Kit (BDK1.1) Step 2: Running the BeanBox tool . change to directory c:\bdk\beanbox and execute the run.bat file.

The Bean Box tool appears in design mode displaying three windowsthe Toolbox, Bean Box and properties Window.

Step 3: Creating an Application The application consists of three beans that are supplied with the beanbox tool-

a juggler bean and two OurButton beans

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, indicates that the bean has the focus.

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.

Tool Box

Bean Box

Properties

Step 4: Connecting Bean


Step 5: Running in Runtime mode Step 6: Saving and Restoring an Application

MOLECULE BEAN AND OURBUTTON BEAN

From the Event Target Dialog box select the Rotate on X method and Rotate on Y method accordingly. And click OK button.

Property
Events Introspection

Customization
Persistence

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 bean properties mechanism. Also called getter and setter.

get method used to read values of the propery.


set method sets the value of the property. Property is a subset of Beans state.

It determines the behavior and appearance of that component

Bean properties are equivalent to the data fields in java object except that bean properties must declare as private data fields and they must be accessible through special public methods known as accessor methods.

Accessor methods are public methods used to get the value of a beans property. All accessor method names start with the get prefix.
Typically the get prefix is followed by the property name.

Example: - A simple accessor method for a back ground property should like the following

Public Color getBackgroundColor() { Return backgroundColor; }

Public methods used to set the value of a Beans property. Starts with the set prefix followed by the property name. public void setBackgroundColor(Color color) { backgroundColor=color; repaint(); }

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 that change. So the bean is repainted.

When an objects a Bean?


A simple java object becomes a bean when all of the objects data fields are private and are accessible through methods known as accessor methods.

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.

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.

Persistence: - enables developers to customize beans in an application builder Retrieve those beans with customized features Persistence means: Bean properties should remain the same, until they are explicitly changed through a customization mechanism. For example, suppose we change the OurButton beans(bean created by the user) Label property from Press to Start, its value remain same as Start unless you decide to change it again.

Simple

Indexed
Bound Constrained

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

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

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

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

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 propertys array data type.

Example: -

public void setValues(double[] v) { values = v; }

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 is a bound property. When the filename changes the bean automatically notified and it loads a new file.

Mechanism for implementing bound property


Whenever the value of a property changes, the bean must send a PropertyChangeEvent to all registered

listeners.

To enable interested listeners to register themselves the bean has to implement the following two methods.
void addPropertyChangeListener(PropertyChangeListener listener) void removePropertyChangeListener(PropertyChangeListener listener)

java.beans package has a convenient class called PropertyChangeSupport that manage the listeners for you.

To use this class your bean must have a datafield of this like this: private PropertyChangeSupport change=new

PropertyChangeSupport(this)

public void addPropertyChangeListener(PropertyChangeListener listener) { change.addPropertyChangeListener(listener); }


public void removePropertyChangeListener(PropertyChangeListener listener) { change.removePropertyChangeListener(listener); }

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

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)

Java.beans.VetoableChangeListener is the listener interface for receiving changed beans constrained property.
public void addVetoableChangeListener(VetoableChangeListener listener) public void removeVetoableChangeListener(VetoableChangeListener listener)

public void addVetoableChangeListener(VetoableChangeListener listener) { vetosupport.addVetoableChangeListener(listener); }


public void removeVetoable ChangeListener(VetoableChangeListener listener){ change.remove VetoableChangeListener(listener); }

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, and events

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.

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

Step 2 :Compile the Bean using Javac Hello.java and it 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 part of the JAR file.

Step 4: Create the executable JAR file. Use the form of the jar command 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

Step1: Create the directory C:\counter and store the java source file in this directory Step 2:Write the Java Source file and compile it

//counter.java import 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();} };

count=0; setSize(200,100); } public void clicked() { count++; repaint(); }

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

Step 3 : Create the Manifest file counter.mft with the following 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

BeanInfo interface allows a designer to specify information about the properties, events and methods of a bean. This interface defines several methods, including
PropertyDescriptor[] getPropertyDescriptors() EventSetDescriptor[] getEventSetDescriptors() MethodDescriptors[] getMethodDescriptors()

Following are some of the classes defined in Java.beans package.

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.

For Example our ColorsBeanInfo class starts like this: Public class ColorsBeanInfo extends SimpleBeanInfo { Public EventSetDescriptor[] getEventSetDescriptors() { .. } .. }

PropertyDescriptor constructor that is used is shown below:

PropertyDescriptor(String property ,class beanclass) throws IntrospectionException

You might also like