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.
1. JavaBeans
ByBy
Shravan Kumar UpadhayayShravan Kumar Upadhayay
Department of Computer Science &Department of Computer Science &
EngineeringEngineering
Punjab Institute of Technology, KapurthalaPunjab Institute of Technology, Kapurthala
(Punjab Technical University Main Campus)(Punjab Technical University Main Campus)
2. 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
3. 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.
4. 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.
5. 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.
6. 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.
7. Once you have built a bean, users of any
environment enabled for JavaBeans technology
can readily use it.
8. 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.
9. 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.
10. The core classes and interfaces in the packagesThe core classes and interfaces in the packages
java.beans and java.beans.beancontext providejava.beans and java.beans.beancontext provide
the support for creating java beans.the support for creating java beans.
There is no JavaBean super class that all javaThere is no JavaBean super class that all java
beans extend and no interface that all java beansbeans extend and no interface that all java beans
implement.implement.
Creating a class of java beans largely involvesCreating a class of java beans largely involves
adhering to the standards of the java beanadhering to the standards of the java bean
component model.component model.
11. Elements of Java Bean InterfaceElements of Java Bean Interface
Methods: A method represents some action that can beMethods: A method represents some action that can be
executed against the JavaBean. Example: a java beanexecuted against the JavaBean. Example: a java bean
that containsan animation may have method to startthat containsan animation may have method to start
and stop the animation.and stop the animation.
Properties: A property represents an attribute of javaProperties: A property represents an attribute of java
bean, such as its color or font. The property does notbean, such as its color or font. The property does not
have to be a visible attribute. For example: a propertyhave to be a visible attribute. For example: a property
can be an abstract quality, such as a boolean flag thatcan be an abstract quality, such as a boolean flag that
indicates whether a component is enables for input.indicates whether a component is enables for input.
12. Events: java bean objects use events to notifyEvents: java bean objects use events to notify
other java bean objects that some event hasother java bean objects that some event has
occurred. These objects use the same event-occurred. These objects use the same event-
handling mechanism as Swing and AWThandling mechanism as Swing and AWT
components.components.
13. Java beans that must be aware of a certain event registerJava beans that must be aware of a certain event register
as a listener with java bean that generates the event.as a listener with java bean that generates the event.
Listener java beans must implement the interface thatListener java beans must implement the interface that
corresponds to the event class of interest.corresponds to the event class of interest.
Source java beans provides registration methods for theSource java beans provides registration methods for the
event. When the event occurs, the source java beanevent. When the event occurs, the source java bean
sends a copy of the event to each registered listener.sends a copy of the event to each registered listener.
Many of the events generated by beans areMany of the events generated by beans are
PropertyChangeEvent objects, but you can definePropertyChangeEvent objects, but you can define
custom events.custom events.
14. 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.
15. 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.
16. Fortunately, you need to master only a small
number of concepts to write beans with a rich
set of behaviors.
17. Main characteristicsMain characteristics
If a bean has a property named X, it can haveIf a bean has a property named X, it can have
public methods named setX and getX, to assignpublic methods named setX and getX, to assign
and return the value of the property X.and return the value of the property X.
For boolean type properties methods are setXFor boolean type properties methods are setX
and isX.and isX.
18. All beans should have a constructor that takesAll beans should have a constructor that takes
no arguments because most beanboxes call thisno arguments because most beanboxes call this
constructor.constructor.
19. 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.
20. 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.
21. 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.
22. The naming pattern for properties is simple: AnyThe naming pattern for properties is simple: Any
pair of methodspair of methods
public X getPropertyName()public X getPropertyName()
public void setPropertyName(X x)public void setPropertyName(X x)
corresponds to a read/write property of type X.corresponds to a read/write property of type X.
23. 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.
24. 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.
25. 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.
26. public void setFileName(String f)
{
fileName = f;
image = . . .
repaint();
}
public String getFileName()
{
if (file == null) return null;
else return file.getPath();
}
27. 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.
28. They must follow the pattern:
X[] getPropertyName()
void setPropertyName(X[] x)
X getPropertyName(int i)
void setPropertyName(int i, X x)
30. To implement a bound property, you must implement twoTo implement a bound property, you must implement two
mechanisms.mechanisms.
Whenever the value of the property changes, the bean must sendWhenever the value of the property changes, the bean must send
a PropertyChange event to all registered listeners. This changea PropertyChange event to all registered listeners. This change
can occur when the set method is called or when the programcan occur when the set method is called or when the program
user carries out an action, such as editing text or selecting a file.user carries out an action, such as editing text or selecting a file.
To enable interested listeners to register themselves, the bean hasTo enable interested listeners to register themselves, the bean has
to implement the following two methods:to implement the following two methods:
Void addPropertyChangeListener(PropertyChangeListener listener)Void addPropertyChangeListener(PropertyChangeListener listener)
Void removePropertyChangeListener(PropertyChangeListener listener)Void removePropertyChangeListener(PropertyChangeListener listener)
31. The java.beans package has a convenience class,
called PropertyChangeSupport, that manages the
listeners for you.
32. 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.
34. Constrained PropertiesConstrained Properties
A constrained property is the most interesting ofA constrained property is the most interesting of
the properties, and also the most complex tothe properties, and also the most complex to
implement. Such a property is constrained byimplement. Such a property is constrained by
the fact that any listener can "veto" proposedthe fact that any listener can "veto" proposed
changes, forcing it to revert to the old setting.changes, forcing it to revert to the old setting.
35. 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.
36. Write a class CustomEvent that extends EventObject.Write a class CustomEvent that extends EventObject.
(The event name must end in Event in order for a(The event name must end in Event in order for a
builder to use the naming patterns to find it.)builder to use the naming patterns to find it.)
Write an interface CustomListener with one or moreWrite an interface CustomListener with one or more
notification methods. Those methods can have anynotification methods. Those methods can have any
name, but they must have a single parameter of typename, but they must have a single parameter of type
CustomEvent and return type void.CustomEvent and return 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 e)public void removeCustomListener(CustomListener e)
37. 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.
38. 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.