Java beans

Preview:

Citation preview

Advanced Java Programming

Topic: Java Beans

ByRavi Kant Sahu

Asst. Professor, LPU

Introduction

Every Java user interface class is a JavaBeans component.

Java Beans makes it easy to reuse software components.

Developers can use software components written by others without having to understand their inner workings.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Java Beans

JavaBeans is a software component architecture that extends the power of the Java language by enabling well-formed objects to be manipulated visually at design time in a pure Java builder tool.

It is a reusable software component.

A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans API specifications.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Java Beans Specifications1. A bean must be a public class.2. A bean must have a public no-arg constructor, though it can have

other constructors if needed.public MyBean();

3. A bean must implement the Serializable interface to ensure a persistent state.

4. It should provide methods to set and get the values of the properties, known as getter (accessor) and setter (mutator) methods.

5. A bean may have events with correctly constructed public registration and deregistration methods that enable it to add and remove listeners.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Java Beans Specification

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

The first three requirements must be observed, and therefore are referred to as minimum JavaBeans component requirements.

The last two requirements depend on implementations.

It is possible to write a bean component without get/set methods and event registration/deregistration methods.

Java Beans Examplepublic class StudentsBean implements java.io.Serializable

{ private String firstName = null; private String lastName = null; private int age = 0; public StudentsBean() { } public String getFirstName() { return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public void setFirstName(String firstName)

{ this.firstName = firstName; } public void setLastName(String lastName)

{ this.lastName = lastName; } public void setAge(Integer age){ this.age = age; } }

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Java Beans Component The classes that define the beans, referred to as JavaBeans

components or bean components.

A JavaBeans component is a serializable public class with a public no-arg constructor.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Properties are discrete, named attributes of a Java bean that can affect its appearance or behavior.

They are often data fields of a bean. For example, JButton component has a property named text.

Accessor and mutator methods are provided to let the user read and write the properties.

Bean Properties

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Property-Naming Patterns The bean property-naming pattern is a convention of the

JavaBeans component model that simplifies the bean developer's task of presenting properties.

A property can be a primitive data type or an object type.

The property type dictates the signature of the accessor and mutator methods.

Note: Properties describe the state of the bean. Naturally, data fields are used to store properties.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

The accessor method is named get<PropertyName>(), which takes no parameters and returns a primitive type value or an object of a type identical to the property type.

public String getMessage() public int getXCoordinate() public int getYCoordinate()

Accessor Methods

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

For a property of boolean type, the accessor method should be named

is<PropertyName>( )

which returns a boolean value.

For example:public boolean isCentered()

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

The mutator method should be named asset<PropertyName>(dataType p)

which takes a single parameter identical to the property type and returns void.

public void setMessage(String s) public void setXCoordinate(int x) public void setYCoordinate(int y) public void setCentered(boolean centered)

Mutator Methods

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Java EvEnt ModEl

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Event Delegation Model The Java event delegation model provides the foundation for

beans to send, receive, and handle events.

The Java event model consists of the following three types of elements:

The event object: An event object contains the information that describes the event.

The source object: A source object is where the event originates. When an event occurs on a source object, an event object is created.

The event listener object: An object interested in the event handles the event. Such an object is called a listener.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Event classes and Event Listener interfaces An event object is created using an event class, such as

ActionEvent, MouseEvent, and ItemEvent.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Source Components The source component contains the code that detects an

external or internal action that triggers the event.

Upon detecting the action, the source should fire an eventto the listeners by invoking the event handler defined bythe listeners.

The source component must also contain methods forregistering and deregistering listeners.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Listener Components A listener component for an event must implement the event

listener interface.

The object of the listener component cannot receive eventnotifications from a source component unless the object isregistered as a listener of the source.

A listener component may implement any number of listenerinterfaces to listen to several types of events.

A source component may register many listeners. A sourcecomponent may register itself as a listener.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Creating Custom Source Components

A source component must have the appropriateregistration and deregistration methods for adding andremoving listeners.

Events can be unicasted (only one listener object isnotified of the event) or multicasted (each object in alist of listeners is notified of the event).

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Unicastpublic void add<Event>Listener(<Event>Listener l)

throws TooManyListenersException;

MulticastThe naming pattern for adding a multicast listener is the same,except that it does not throw the TooManyListenersException.

public void add<Event>Listener(<Event>Listener l)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Deregistration Method

The naming pattern for removing a listener (either unicast or multicast)public void remove<Event>Listener(<Event>Listener l)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Creating Java Beans

Step 1: Create a Java Bean file e.g. “LightBulb.java”Step 2: Compile the file: javac LightBulb.javaStep 3: Create a manifest file, named “manifest.tmp”Step 4: Create the JAR file, named “LightBulb.jar”Step 5: Load jar file.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Manifest File Create text file manifest.tmp Manifest file describes contents of JAR file

Name: name of file with bean class Java-Bean: true - file is a JavaBean

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Creating Jar file

c - creating JAR filef - indicates next argument is name of filem - next argument manifest.tmp file

Used to create MANIFEST.MF

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

.

.