0% found this document useful (0 votes)
13 views

Unit 2.2_Java Beans

Uploaded by

fortiratra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit 2.2_Java Beans

Uploaded by

fortiratra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

JDBC Database Connectivity

Enterprise Application Development

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,

• Java Beans : Introduction, JavaBeans Component Model,


Bean Development Environments, Using the Sun
BeanBox, Creating a JavaBean Class, Exploring JavaBean
Property Types, Adding Custom Event Types, Creating a
JavaBean Class with Events, Using the BeanInfo Classes.
4/18/2023 2
INTRODUCTION TO JAVA BEANS
• Visual Programming Languages (such as Visual Basic) have been very
popular in building GUI applications. In visual programming, you can drag
and drop a visual component into a Application Builder and attach event
handler to the component.
• Visual programming is ideal for rapid prototyping of GUI applications.
Visual programming relies on component and event-driven technology.
• Components are reusable software units that can be assembled into an
application via an application building tool (e.g., Visual Studio, JBuilder,
NetBeans, Eclipse).

• In Java, visual programming is supported via the "Javabean" API. The


application builder tool loads the beans into a "toolbox" or "palette".

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.“

• Software components are self-contained software units developed according to


the motto “Developed them once, run and reused them everywhere”. Or in
other words, reusability is the main concern behind the component model.

• "A Javabean is an independent, reusable software component. Beans may be


visual object, like Swing components (e.g. JButton, JTextField) that you can
drag and drop using a GUI builder tool to assemble your GUI application.

• A software component is a reusable object that can be plugged into any target
software application.

• You can develop software components using various programming languages,


such as C, C++, Java, and Visual Basic.
4/18/2023 4
INTRODUCTION TO JAVA BEANS
• A “Bean” is a reusable software component model based on sun’s java
bean specification that can be manipulated visually in a builder tool.

• The term software component model describe how to create and use
reusable software components to build an application.

• Builder tool is nothing but an application development tool which lets


you both to create new beans or use existing beans to create an
application.

• Java provides the facility of creating some user defined components by


means of Bean programming.

4/18/2023 5
Advantages of Java Beans:
• The java beans posses the property of “Write once and run anywhere”.

• Beans can work in different local platforms.

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

• The configuration setting can be made persistent.(reused)

• Configuration setting of a bean can be saved in persistent storage and


restored later.
4/18/2023 6
What can we do/create by using JavaBean:
• There is no restriction on the capability of a Bean.
• It may perform a simple function, such as checking the spelling of a
document, or a complex function, such as forecasting the performance
of a stock portfolio.
• A Bean may be visible to an end user. One example of this is a button
on a graphical user interface.
• Software to generate a pie chart from a set of data points is an example
of a Bean that can execute locally.

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

A JavaBeans component is a Java class with the following features:


•A no-argument constructor.
•Properties defined with accessors and mutators(getter and setter
method).
•Class must not define any public instance variables.
•The class must implement the java.io.Serializable interface.

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.

Services of JavaBean Components


• Builder support:- Enables you to create and group multiple JavaBeans in
an application.
• Layout:- Allows multiple JavaBeans to be arranged in a development
environment.
• Interface publishing: Enables multiple JavaBeans in an application to
communicate with each other.
• Event handling:- Refers to firing and handling of events associated with a
JavaBean.
4/18/2023 14
Bean Development Environments
Beans Development Kit
Is a development environment to create, configure, and test JavaBeans.

The features of BDK environment are:


• Provides a GUI to create, configure, and test JavaBeans.
• Enables you to modify JavaBean properties and link multiple
JavaBeans in an application using BDK.
• Provides a set of sample JavaBeans.
• Enables you to associate pre-defined events with sample JavaBeans.

Identifying BDK Components

• The components of BDK development environment are:


• ToolBox
• BeanBox
• Properties
• Method Tracer

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

A JavaBeans Class should have following properties:

1. It should have no-arg constructor- Default constructor

2. It should be serializable. Beans should implement java.io.serializable, as


it allows to save, store and restore the state of JavaBeans you are working
on.

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

public void setId(int id) {


this.id = id;
}

public int getId() {


return id;
}

public void setName(String name) {


this.name = name;
}

public String getName() {


return name;
}
}
4/18/2023 26
Program to access JavaBeans:

public class Emp1 {


public static void main(String args[]) {
Employee e = new Employee();
e.setId(101);
e.setName("Ram");
System.out.println(e.getId());
System.out.println(e.getName());
}
}

4/18/2023 27
Exploring JavaBean Property Types

• A property is a subset of a Bean’s state.


• 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.
• Properties are the private data members of the JavaBean classes.
• Properties are used to accept input from an end user in order to customize a
JavaBean.
• Properties can retrieve and specify the values of various attributes, which
determine the behaviour of a JavaBean.

Types of JavaBeans Properties


• Simple properties
• Boolean properties
• Indexed properties

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 .

The syntax of get method is:


public return_type get<PropertyName>
public T getN();

4/18/2023 29
Exploring JavaBean Property Types: Simple Properties:

Ex:
public double getDepth()
{
return depth;
}

Read only property has only a get method.

The syntax of set method is:


public void set<PropertyName>(data_type value)

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.

It can identified by the following methods:


Syntax:
Let N be the name of the property and T be the type of the value then
public boolean isN();
public Boolean getN();
public void setN(boolean parameter);

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.

• An indexed property may expose set/get methods to read/write one element


in the array (so-called ’index getter/setter’) and/or so-called ’array
getter/setter’ which read/write the entire array.

• 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:

Indexed Properties are retrieved using the following get methods:

Syntax: public int[] get <PropertyName>()


Ex:
private double data[];
public double getData(int index)
{
return data[index];
}

Syntax: public property_datatype get <PropertyName>(int index)


Ex:
public void setData(int index,double value)
{
Data[index]=value;
}

4/18/2023 34
Exploring JavaBean Property Types: Indexed Properties:

Indexed Properties are specified using the following set methods:

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

You might also like