introduction of java beans

39
JavaBeans By By Shravan Kumar Upadhayay Shravan Kumar Upadhayay Department of Computer Science & Department of Computer Science & Engineering Engineering Punjab Institute of Technology, Punjab Institute of Technology, Kapurthala Kapurthala (Punjab Technical University Main

Upload: shravan-upadhayay

Post on 12-Jun-2015

165 views

Category:

Education


2 download

DESCRIPTION

A bean is a reusable software component based on Sun's JavaBeans specification that can be manipulated visually in a builder tool.“ The JavaBeans technology enables vendors to create environments that make it dramatically easier to develop user interfaces for Java applications.

TRANSCRIPT

Page 1: introduction of Java beans

JavaBeans

By By

Shravan Kumar Upadhayay Shravan Kumar Upadhayay

Department of Computer Science & Department of Computer Science & EngineeringEngineering

Punjab Institute of Technology, Punjab Institute of Technology, KapurthalaKapurthala

(Punjab Technical University Main (Punjab Technical University Main Campus)Campus)

Page 2: introduction of Java beans

Why Beans? The Bean-Writing Process Using Beans to Build an Application Naming Patterns for Bean Properties and

Events Bean Property Types Adding Custom Bean Events Property Editors Going Beyond Naming Patterns Customizers The Bean Context

Page 3: introduction of Java beans

BeanBean

"A bean is a reusable software component based on Sun's JavaBeans specification that can be manipulated visually in a builder tool.“

The JavaBeans technology enables vendors to create environments that make it dramatically easier to develop user interfaces for Java applications.

Page 4: introduction of Java beans

Once you implement a bean, others can use it in a builder environment such as Forte, JBuilder, VisualAge, or Visual Café to produce applications or applets more efficiently.

Page 5: introduction of Java beans

VBVB

You build the interface by dropping components (called controls in Visual Basic) onto a form window.

Through property sheets, you set properties of the components such as height, color, or other behavior.

The property sheets also list the events to which components can react. For some of those events, you write short snippets of event handling code.

Page 6: introduction of Java beans

VB & BeanVB & Bean

1. Beans with the same functionality as the most common Visual Basic controls are readily available.

2. Java technology builder tools have come much closer to the ease of use of the Visual Basic environment.

Page 7: introduction of Java beans

Once you have built a bean, users of any environment enabled for JavaBeans technology can readily use it.

Page 8: introduction of Java beans

First, clear that writing a bean is not technically difficult—there are only a few new classes and interfaces for you to master.

In particular, the simplest kind of bean is really nothing more than a Java platform class that follows some fairly strict naming conventions for its event listeners and its methods.

Page 9: introduction of Java beans

The Bean-Writing Process

All accessor methods begin with get, all setter methods begin with set.

builder tools use this standard naming convention to discover properties.

Properties are conceptually at a higher level than instance variables—they are features of the interface, whereas instance variables belong to the implementation of the class.

Page 10: introduction of Java beans

The core classes and interfaces in the The core classes and interfaces in the packages java.beans and packages java.beans and java.beans.beancontext provide the java.beans.beancontext provide the support for creating java beans.support for creating java beans.

There is no JavaBean super class that There is no JavaBean super class that all java beans extend and no interface all java beans extend and no interface that all java beans implement.that all java beans implement.

Creating a class of java beans largely Creating a class of java beans largely involves adhering to the standards of involves adhering to the standards of the java bean component model.the java bean component model.

Page 11: introduction of Java beans

Elements of Java Bean Elements of Java Bean InterfaceInterface

Methods: A method represents some action Methods: A method represents some action that can be executed against the JavaBean. that can be executed against the JavaBean. Example: a java bean that containsan Example: a java bean that containsan animation may have method to start and stop animation may have method to start and stop the animation.the animation.

Properties: A property represents an attribute Properties: A property represents an attribute of java bean, such as its color or font. The of java bean, such as its color or font. The property does not have to be a visible attribute. property does not have to be a visible attribute. For example: a property can be an abstract For example: a property can be an abstract quality, such as a boolean flag that indicates quality, such as a boolean flag that indicates whether a component is enables for input.whether a component is enables for input.

Page 12: introduction of Java beans

Events: java bean objects use events Events: java bean objects use events to notify other java bean objects that to notify other java bean objects that some event has occurred. These some event has occurred. These objects use the same event-handling objects use the same event-handling mechanism as Swing and AWT mechanism as Swing and AWT components. components.

Page 13: introduction of Java beans

Java beans that must be aware of a certain Java beans that must be aware of a certain event register as a listener with java bean that event register as a listener with java bean that generates the event. generates the event.

Listener java beans must implement the Listener java beans must implement the interface that corresponds to the event class interface that corresponds to the event class of interest. of interest.

Source java beans provides registration Source java beans provides registration methods for the event. When the event occurs, methods for the event. When the event occurs, the source java bean sends a copy of the event the source java bean sends a copy of the event to each registered listener. Many of the events to each registered listener. Many of the events generated by beans are PropertyChangeEvent generated by beans are PropertyChangeEvent objects, but you can define custom events.objects, but you can define custom events.

Page 14: introduction of Java beans

1. Beans need to be usable by less-than-expert programmers. Such people will access most of the functionality of your bean with a visual design tool that uses essentially no programming. You need to expose lots of properties to make it easy to customize the bean behavior.

2. The same bean needs to be usable in a wide variety of contexts. A bean that is too simplistic to be usable in the real world won't get much respect. Of course, that is why people can charge hundreds of dollars for a professional component, such as a full-featured chart control.

Page 15: introduction of Java beans

If you are writing a program for a single application, then you will simply choose one behavior or the other. But if you are building a bean, you aren't programming for yourself but for a wide audience of users with different needs.

Therefore, it would be best to add a property called something like incrementalUpdate and implement both behaviors, depending on the value of that property.

Page 16: introduction of Java beans

Fortunately, you need to master only a small number of concepts to write beans with a rich set of behaviors.

Page 17: introduction of Java beans

Main characteristicsMain characteristics

If a bean has a property named X, it If a bean has a property named X, it can have public methods named setX can have public methods named setX and getX, to assign and return the and getX, to assign and return the value of the property X. value of the property X.

For boolean type properties methods For boolean type properties methods are setX and isX.are setX and isX.

Page 18: introduction of Java beans

All beans should have a constructor All beans should have a constructor that takes no arguments because that takes no arguments because most beanboxes call this most beanboxes call this constructor.constructor.

Page 19: introduction of Java beans

Using Beans to Build an Application

In particular, the only way to use Java Beans in an ordinary program in the Java programming language would be to write code that constructs an object of the bean class, places the object into a container, and calls the setProperty method.

Page 20: introduction of Java beans

Packaging Beans in JAR files

To make any bean usable in a builder tool, package all class files that are used by the bean code into a JAR file.

Unlike the JAR files for an applet that you saw previously, a JAR file for a bean needs a manifest file that specifies which class files in the archive are beans and should be included in the Toolbox.

Page 21: introduction of Java beans

Naming Patterns for Bean Properties and Events

First, clear that there is no cosmic beans class that you extend to build your beans. Visual beans directly or indirectly extend the Component class, but no-nvisual beans don't have to extend any particular superclass.

A bean is simply any class that can be manipulated in a builder tool. The builder tool does not look at the superclass to determine the bean nature of a class, but it analyzes the names of its methods. To enable this analysis, the method names for beans must follow certain patterns.

Page 22: introduction of Java beans

The naming pattern for properties is The naming pattern for properties is simple: Any pair of methodssimple: Any pair of methods public X getPropertyName()public X getPropertyName() public void setPropertyName(X x)public void setPropertyName(X x)

corresponds to a read/write property corresponds to a read/write property of type X.of type X.

Page 23: introduction of Java beans

Bean Property Types

A sophisticated bean will have lots of different kinds of properties that it should expose in a builder tool for a user to set at design time or get at run time.

It can also trigger both standard and custom events.

Page 24: introduction of Java beans

Getting the properties of your beans right is probably the most complex part of building a bean because the model is quite rich. The JavaBeans specification allows four types of properties.

Page 25: introduction of Java beans

Simple Properties

A simple property is one that takes a single value such as a string or a number.

Simple properties are easy to program: just use the set/get naming convention we indicated earlier.

Page 26: introduction of Java beans

public void setFileName(String f){fileName = f;image = . . .repaint();}public String getFileName(){if (file == null) return null;else return file.getPath();}

Page 27: introduction of Java beans

Indexed Properties

An indexed property is one that gets or sets an array. With an indexed property, you supply two pairs of get and set methods: one for the array and one for individual entries.

Page 28: introduction of Java beans

They must follow the pattern: X[] getPropertyName() void setPropertyName(X[] x) X getPropertyName(int i) void setPropertyName(int i, X x)

Page 29: introduction of Java beans

Bound Properties

Bound properties tell interested listeners that their value has changed.

Page 30: introduction of Java beans

To implement a bound property, you must implement To implement a bound property, you must implement two mechanisms.two mechanisms.

Whenever the value of the property changes, the bean Whenever the value of the property changes, the bean must send a PropertyChange event to all registered must send a PropertyChange event to all registered listeners. This change can occur when the set method listeners. This change can occur when the set method is called or when the program user carries out an is called or when the program user carries out an action, such as editing text or selecting a file.action, such as editing text or selecting a file.

To enable interested listeners to register themselves, To enable interested listeners to register themselves, the bean has to implement the following two methods:the bean has to implement the following two methods: Void addPropertyChangeListener(PropertyChangeListener Void addPropertyChangeListener(PropertyChangeListener

listener)listener) Void removePropertyChangeListener(PropertyChangeListener Void removePropertyChangeListener(PropertyChangeListener

listener)listener)

Page 31: introduction of Java beans

The java.beans package has a convenience class, called PropertyChangeSupport, that manages the listeners for you.

Page 32: introduction of Java beans

To use this convenience class, your bean must have a data field of this class that looks like this: private PropertyChangeSupport

changeSupport = new PropertyChangeSupport(this);

You delegate the task of adding and removing property change listeners to that object.

Page 33: introduction of Java beans

public void addPropertyChangeListener(PropertyChangeListener listener)

{changeSupport.addPropertyChangeListener(listener);}public void

removePropertyChangeListener(PropertyChangeListener listener)

{changeSupport.removePropertyChangeListener(listener);}

Page 34: introduction of Java beans

Constrained PropertiesConstrained Properties

A constrained property is the most A constrained property is the most interesting of the properties, and interesting of the properties, and also the most complex to implement. also the most complex to implement. Such a property is constrained by Such a property is constrained by the fact that any listener can "veto" the fact that any listener can "veto" proposed changes, forcing it to proposed changes, forcing it to revert to the old setting.revert to the old setting.

Page 35: introduction of Java beans

Adding Custom Bean Events

When you add a bound or constrained property to a bean, you also enable the bean to fire events whenever the value of that property changes.

There are events that a bean can send out, for example,

When the program user has clicked on a control within the bean;

When new information is available; Or, simply when some amount of time has

elapsed.

Page 36: introduction of Java beans

Write a class CustomEvent that extends Write a class CustomEvent that extends EventObject. (The event name must end in EventObject. (The event name must end in Event in order for a builder to use the naming Event in order for a builder to use the naming patterns to find it.)patterns to find it.)

Write an interface CustomListener with one or Write an interface CustomListener with one or more notification methods. Those methods can more notification methods. Those methods can have any name, but they must have a single have any name, but they must have a single parameter of type CustomEvent and return parameter of type CustomEvent and return type void.type void.

Supply the following two methods in the bean:Supply the following two methods in the bean: public void addCustomListener(CustomListener e)public void addCustomListener(CustomListener e) public void removeCustomListener(CustomListener public void removeCustomListener(CustomListener

e)e)

Page 37: introduction of Java beans

Property Editors If you add an integer or string property to a bean,

then that property is automatically displayed in the bean's property inspector.

if you add a property whose values cannot easily be edited in a text field, for example, a date or a Color? Then, you need to provide a separate component that the user can use to specify the property value.

Such components are called property editors. For example, a property editor for a date object

might be a calendar that lets the user scroll through the months and pick a date. A property editor for a Color object would let the user select the red, green, and blue components of the color.

Page 38: introduction of Java beans

Customizers

A property editor, no matter how sophisticated, is responsible for allowing the user to set one property at a time.

Especially if certain properties of a bean relate to each other, it may be more user friendly to give users a way to edit multiple properties at the same time.

To enable this feature, you supply a customizer instead of (or in addition to) multiple property editors.

Page 39: introduction of Java beans