Unit 3 Java Beans
Unit 3 Java Beans
Java Beans
Introduction to Java Beans
A java beans is a software component that has been designed to be reusable in a
variety of different environments.
Beans are important because they allow you to build complex system from software
component.
Java beans defines an architectures that satisfies how these building blocks can
operate together.
Introduction to Java Beans
A simple java object must support the following features for it to become a Java Bean.
Customization
Communication
Persistence
Introspection
Customization
It is the ability of JavaBeans to allow its properties to be changed during the build and
execution phases.
Communication
It is ability of JavaBeans to inform changes in its attributes to other beans and to the
container application.
Introduction to Java Beans
Persistence
It is the ability of java beans to save its state to a disk or any other storage device
when the execution of container application that hold the beans is terminated.
Introspection
• Properties are used to accept input from an end user in order to customize a
java beans
• Simple Properties
• Boolean Properties
• Indexed Properties
Simple Properties
• Simple properties refers to the private variables of
JavaBean that can have only single value.
• Simple properties are retrieved and specified using the
get and set methods respectively.
• The syntax of get and set method:
Set method: public void setN( T arg)
Get method: public T getN()
Syntax:
• For getting values isN() and getN() methods are used and for setting
the Boolean values setN() method is used.
Example:
public boolean dotted=false;
public boolean isDotted()
{
return dotted;
}
public void setDotted(Boolean dotted)
{
this.dotted=dotted;
}
Indexed Properties:
the event.
Constrained properties
Control beans are used to create GUI components that can be plugged into any
application.
Container beans:
You can create a container bean by extending one of the swing container
classes.
These are used to create components that perform specific task in background
of an application.
Accessor and Mutator Methods
Accessor and mutator methods are used to make the properties of bean
by external applications.
You can control which properties, events, and methods of bean are available to an
application builder.
The configuration settings of bean can be saved in persistent storage and restored at
later time.
A bean may register to receive events from other object and can generate events
that are sent to other objects.
Class vs. Beans
JavaBeans are classes that encapsulate many objects into a
single object (the bean). They are serializable, have a zero-
argument constructor, and allow access to properties using
getter and setter methods.
A class is nothing but a blueprint or a template for creating
different objects which defines its properties and
behaviors. Java class objects exhibit the properties and
behaviors defined by its class. A class can contain fields and
methods to describe the behavior of an object.
Bean Development Kit(BDK) and Bean Box
BDK is provided separately from JDK.
BDK uses the tool called Bean Box for Bean creation.
You can create a java bean and then use the bean box to test that it
runs properly.
If a java bean runs properly in bean box, you can be sure that it
ToolBox Window
It display the java beans that are currently installed in the bean box.
When the bean box start, it automatically loads its ToolBox with bean in jar
You can add additional beans such as your beans, to the ToolBox.
BeanBox Window
Use this empty window sometime referred as a “form” by other builder tools,
If no bean is selected such as when you first start bean box, then the
7) Test
Creating a New Java Bean
Create a Directory for the New Bean (You can use netbeans and create a new project for that)
Class should have public default constructor and properties should be private.
Each bean property have getter and setter methods to get and set values of the properties.
Compile the source code to create a class file. Type the following:
The manifest file contains a list of all the files that make
up a java Bean
Java-Bean: True
A JAR file is similar to zip file and contains all the class file of an application.
The JAR file contains the manifest file and all other files such as the class files and
picture files of the bean.
The target application unpackaged the JAR file, read the manifest file first and then
loads the all class files of the bean.
CD to c:\Program Files\BDK1.1\beanbox\.
Then type "run".
Load JAR into Bean Box by selecting "LoadJar..." under the File menu.
Test
After the file selection dialog box is closed, change focus to the "ToolBox"
window. You'll see “MyButton" appear at the bottom of the toolbox
window.
Select MyButton.jar.
Cursor will change to a plus. In the middle BeanBox window, you can now
click to drop in what will appear to be a colored rectangle.
Example: simple MyButton component that you may be use like a JButton in your application
import javax.swing.*;
public class MyButton extends JButton implements Serializable{
public MyButton()
{
super();
}
}
//Target Application
import javax.swing.*;
public class MyTargetApp extends JFrame
{
public MyTargetApp()
{
MyButton btn=new MyButton();
add(btn);
setVisible(true);
setSize(400,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main()
{
new MyTargetApp();
}
}
The Java Beans API
• The Java Beans functionality is provided by a set of classes and interfaces
in the java.beans package
The BeanInfo Interface :
• The BeanInfo interface enables to explicitly control what information is
available.
PropertyDescriptor [] getPropertyDescriptor()
EventSetdDescriptor[] getEventSetDescriptor()
MethodDescriptor[] getMethodDescriptor()
if (pd.getPropertyType()
.isAssignableFrom(java.lang.String.class)) {
System.out.println("Property value: " + pd.getReadMethod()
.invoke(instance));
pd.getWriteMethod()
.invoke(instance, "a string value set");
System.out.println("Property value after setting: " +
pd.getReadMethod()
.invoke(instance));
} }
}}