Java Beans Unit 4(Part 1)

454 views 33 slides Feb 02, 2021
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

Java Beans.......


Slide Content

Java Beans Unit 4(Part 1) BY:SURBHI SAROHA

SYLLABUS Java Beans Java Beans component model Bean development environments Creating a Java Bean class Exploring indexed , bound and constrained properties

Java Beans A JavaBean is a Java class that should follow the following conventions: It should have a no- arg constructor. It should be Serializable . It should provide methods to set and get the values of the properties, known as getter and setter methods . // Java program to illustrate the // structure of JavaBean class public class TestBean { private String name; public void setName (String name) {

Cont …. this.name = name; } public String getName () { return name; } }

Cont …. Syntax for setter methods: It should be public in nature. The return-type should be void. The setter method should be prefixed with set. It should take some argument i.e. it should not be no- arg method. Syntax for getter methods : It should be public in nature. The return-type should not be void i.e. according to our requirement we have to give return-type. The getter method should be prefixed with get. It should not take any argument.

Cont … // Java Program of JavaBean class package geeks; public class Student implements java.io.Serializable { private int id; private String name; public Student()      {      } public void setId( int id)      {          this .id = id;      } public int getId()      {          return id;      } public void setName(String name)

Cont … { this.name = name; } public String getName () { return name; } } // Java program to access JavaBean class package geeks; public class Test { public static void main(String args [])

Cont …. { Student s = new Student(); // object is created s.setName ("GFG"); // setting value to the object System.out.println ( s.getName ()); } } Output: GFG

Java Beans component model JavaBeans are introduced in 1996 by Sun Microsystem and defined as “A JavaBean is reusable, platform independent component that can be manipulated visually in a builder tool .” In computing, based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean). Builder tool enables you to create and use beans for application development purpose. In simple words JavaBean is nothing but a Java class. When these JavaBeans are used in other applications, the internal working of such components are hidden from the application developer. Example: All Swing and AWT classes are JavaBeans. GUI components are ideal JavaBeans.

Components of JavaBeans The classes that contained definition of beans is known as components of JavaBeans . These classes follows certain design conventions. It includes properties, events, methods and persistence. There are two types of components, GUI based and non GUI based. For instance JButton is example of a component not a class . Properties ( data members):  Property is a named attribute of a bean, it includes color, label, font, font size, display size. It determines appearance, behavior and state of a bean. Methods:  Methods in JavaBeans are same as normal Java methods in a class. It doesn’t follow any specific naming conventions. All properties should have accessor and getter methods. Events:  Events in JavaBeans are same as SWING/AWT event handling. Persistence:  Serializable interface enables JavaBean to store its state. JavaBean has no argument constructor.

JavaBean component

JavaBeans Properties JavaBean property can be access by the user of the object, it can be read, write, read only or write only. We can access these JavaBeans properties with the help of getPropertyName () method also known as getter or accessor and setPropertyName () method known as setter written in implementation class of bean. GetPropertyName ():  For example, if property name is Title, your method name would be geTitle (). SetPropertyName ():  For example, if property name  is Title, your method name would be setTitle ().

Advantages of JavaBeans Following are some advantages of JavaBeans: Reusability in different environments. Used to create applet, servlet, application or other components. JavaBeans are dynamic, can be customized. Can be deployed in network systems

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 • Execute the run.bat file of BDK to start the BDK development environment.

Cont …. The components of BDK development environment are: 1.ToolBox 2 . BeanBox 3 . Properties 4 . Method Tracer

ToolBox window: Lists the sample JavaBeans of BDK. The following figure shows the ToolBox window:

BeanBox window: Is a workspace for creating the layout of JavaBean application.

Properties window: Displays all the exposed properties of a JavaBean.

Method Tracer window: Displays the debugging messages and method calls for a JavaBean application.

Creating a Java Bean class

The program will instantiate the Java bean and then call the setter and getter methods of the newly created Java bean.

Exploring indexed , bound and constrained properties Indexed Properties An indexed property is an array instead of a single value. In this case, the bean class provides a method for getting and setting the entire array. Here is an example for an int [] property called testGrades : public int [] getTestGrades () { return mTestGrades ; } public void setTestGrades ( int [] tg ) { mTestGrades = tg ; }

Cont …. For indexed properties, the bean class also provides methods for getting and setting a specific element of the array. public int getTestGrades ( int index) { return mTestGrades [index]; } public void setTestGrades ( int index, int grade) { mTestGrades [index] = grade; }

Bound Properties A bound property notifies listeners when its value changes. This has two implications: The bean class includes addPropertyChangeListener () and removePropertyChangeListener () methods for managing the bean's listeners. When a bound property is changed, the bean sends a PropertyChangeEvent to its registered listeners. PropertyChangeEvent and PropertyChangeListener live in the java.beans package. The java.beans package also includes a class, PropertyChangeSupport , that takes care of most of the work of bound properties. This handy class keeps track of property listeners and includes a convenience method that fires property change events to all registered listeners.

Example The following example shows how you could make the mouthWidth property a bound property using PropertyChangeSupport . The necessary additions for the bound property are shown in bold. import java.beans .*; public class FaceBean { private int mMouthWidth = 90; private PropertyChangeSupport mPcs = new PropertyChangeSupport (this); public int getMouthWidth () { return mMouthWidth ; }

Cont … public void setMouthWidth ( int mw) { int oldMouthWidth = mMouthWidth ; mMouthWidth = mw; mPcs.firePropertyChange (" mouthWidth ", oldMouthWidth , mw); } public void addPropertyChangeListener ( PropertyChangeListener listener) { mPcs.addPropertyChangeListener (listener); }

Cont …. public void removePropertyChangeListener ( PropertyChangeListener listener) { mPcs.removePropertyChangeListener (listener); } } Bound properties can be tied directly to other bean properties using a builder tool like NetBeans . You could, for example, take the value property of a slider component and bind it to the mouthWidth property shown in the example. NetBeans allows you to do this without writing any code.

Constrained Properties A constrained property is a special kind of bound property. For a constrained property, the bean keeps track of a set of veto listeners. When a constrained property is about to change, the listeners are consulted about the change. Any one of the listeners has a chance to veto the change, in which case the property remains unchanged. The veto listeners are separate from the property change listeners. Fortunately, the java.beans package includes a VetoableChangeSupport class that greatly simplifies constrained properties. Changes to the mouthWidth example are shown in bold:

Example import java.beans .*; public class FaceBean { private int mMouthWidth = 90; private PropertyChangeSupport mPcs = new PropertyChangeSupport (this); private VetoableChangeSupport mVcs = new VetoableChangeSupport (this ); public int getMouthWidth () { return mMouthWidth ; }

Cont …. public void setMouthWidth ( int mw) throws PropertyVetoException { int oldMouthWidth = mMouthWidth ; mVcs.fireVetoableChange (" mouthWidth ", oldMouthWidth , mw); mMouthWidth = mw; mPcs.firePropertyChange (" mouthWidth ", oldMouthWidth , mw); } public void addPropertyChangeListener ( PropertyChangeListener listener) {

Cont …. mPcs.addPropertyChangeListener (listener); } public void removePropertyChangeListener ( PropertyChangeListener listener) { mPcs.removePropertyChangeListener (listener); } public void addVetoableChangeListener ( VetoableChangeListener listener) { mVcs.addVetoableChangeListener (listener); }

Cont …. public void removeVetoableChangeListener ( VetoableChangeListener listener) { mVcs.removeVetoableChangeListener (listener); } }

Thank you 
Tags