Unit 2.2_Java Beans
Unit 2.2_Java Beans
Bhupendra Panchal
4/18/2023 1
Outlines
• Java Beans: Common Elements of Graphical User
Interfaces: Introduction, Main features and Technology of
GUI, Introducing the Java foundation classes, Event
Model, JFC Sample programs, Layout managers, Events,
4/18/2023 3
INTRODUCTION TO JAVA BEANS
• "A Javabean is a reusable software component that can be manipulated visually
in an application builder tool.“
• A software component is a reusable object that can be plugged into any target
software application.
• The term software component model describe how to create and use
reusable software components to build an application.
4/18/2023 5
Advantages of Java Beans:
• The java beans posses the property of “Write once and run anywhere”.
• Beans have the capability of capturing the events sent by other objects and
vice versa enabling object communication.
• The properties, events and methods of the bean can be controlled by the
application developer.(ex. Add new properties)
4/18/2023 7
Builder Tool
• Builder tools allow a developer to work with JavaBeans in a convenient
way.
• A builder tool expose their features (such as properties, methods,
events) to the application builder tools for visual manipulation.
• A builder tool maintains a list of all JavaBeans available.
• It allows you to compose the Bean into applets, application, servlets and
composite components.
4/18/2023 8
Builder Tool
• Some Examples of Application Builder tools:
4/18/2023 9
JavaBeans basic rules
4/18/2023 10
JavaBeans Components Model
4/18/2023 11
JavaBeans Components Model
4/18/2023 12
JavaBeans Components Model
• Properties
• Similar to instance variables.
• A bean property is a named attribute of a bean that can affect its
behaviour or appearance.
• Examples of bean properties include color, label, font, font size,
and display size.
• Methods
• Same as normal Java methods.
• Every property should have accessor (get) and mutator (set)
method.
• There is no specific naming standard for these methods.
• Events
• Similar to Swing/AWT event handling.
4/18/2023 13
JavaBeans Components Model
The JavaBean Component Specification:
• Customization: Is the ability of JavaBean to allow its properties to be
changed in build and execution phase.
• Persistence:- Is the ability of JavaBean to save its state to disk or storage
device and restore the saved state when the JavaBean is reloaded.
• Communication:-Is the ability of JavaBean to notify change in its
properties to other JavaBeans.
• Introspection:- Is the ability of a JavaBean to allow an external application
to query the properties, methods, and events supported by it.
4/18/2023 15
Bean Development Environments
4/18/2023 16
Bean Development Environments
4/18/2023 17
Bean Development Environments
4/18/2023 18
Bean Development Environments
4/18/2023 19
Bean Development Environments
4/18/2023 20
Creating a JavaBean Class
3. It should provide methods to set and get the values of the properties,
known as setter and getter methods. A getter method is used to read the
value, a setter method is used to update the value.
4/18/2023 21
Creating a JavaBean Class
4/18/2023 22
Creating a JavaBean Class
4/18/2023 23
Creating a JavaBean Class
4/18/2023 24
Creating a JavaBean
4/18/2023 25
Implementation of JavaBeans:
public class Employee implements java.io.Serializable {
private int id;
private String name;
public Employee() {
4/18/2023 27
Exploring JavaBean Property Types
4/18/2023 28
Exploring JavaBean Property Types: Simple Properties:
Simple Properties:
• Simple properties refer to the private variables of a JavaBean that can have
only a single value.
• Simple properties are retrieved and specified using the get and set methods
respectively.
• A read/write property has both of these methods to access its values. The
get method used to read the value of the property .The set method that sets
the value of the property.
• The setXXX() and getXXX() methods are the heart of the java beans
properties mechanism. This is also called getters and setters. These
accessor methods are used to set the property .
4/18/2023 29
Exploring JavaBean Property Types: Simple Properties:
Ex:
public double getDepth()
{
return depth;
}
Ex:
public void setDepth(double d)
{
Depth=d;
}
4/18/2023
Write only property has only a set method. 30
Exploring JavaBean Property Types: Boolean Properties:
Boolean Properties:
A Boolean property is a property which is used to represent the values True or
False.
For getting the values isN() and getN() methods are used and for setting the
Boolean values setN() method is used.
4/18/2023 31
Exploring JavaBean Property Types: Boolean Properties:
Ex:
public boolean curved=false;
public boolean isCurved()
{
return curved;
}
public void setCurved(boolean curved)
{
this.curved=curved;
}
4/18/2023 32
Exploring JavaBean Property Types: Indexed Properties:
Indexed Properties:
• Indexed Properties are consists of multiple values.
• If a simple property can hold an array of value then they called indexed
properties.
• Indexed Properties enable you to set or retrieve the values from an array of
property values.
4/18/2023 33
Exploring JavaBean Property Types: Indexed Properties:
4/18/2023 34
Exploring JavaBean Property Types: Indexed Properties:
Syntax:
public void set <PropertyName>()(int index, property_datatype value)
EX:
public double[] getData()
{ return data; }
Syntax :
public void set <PropertyName>()(property_datatype[] property_array)
Ex:
public void setData(double[] values)
{
}
4/18/2023 35
4/18/2023 36