Professional Documents
Culture Documents
CIS 421
import java.awt.*;
public class VerySimpleBean extends Canvas {
2
Creating a Very Simple Java Bean
3
Properties
Simple Properties:
– Used to represent single attribute,
– Single value at an instance of time
4
Java Bean
import java.awt.*;
(Example 2 – Simple Property: Color)
import java.io.Serializable;
public class SimpleBean extends Canvas implements Serializable
{
private Color color = Color.green;
} // class
6
Properties
Bound Properties:
– A component can choose to provide a change notification
service for some or all of its properties. Such properties are
commonly known as bound properties .
– The PropertyChangeListener event listener interface is used to
report updates to simple bound properties. If a bean supports
bound properties then it should support a normal pair of
multicast event listener registration methods for
PropertyChangeListeners:
public void addPropertyChangeListener (PropertyChangeListener x);
public void removePropertyChangeListener (PropertyChangeListener x);
7
Properties
Bound Properties contd…
– The event source should fire the event after updating its internal
state
– For programming convenience, a utility class
PropertyChangeSupport is provided that can be used to keep
track of PropertyChangeListeners and to fire PropertyChange
events.
– Example: BoundProperty.java
Font is bound
8
Properties
Constrained Properties:
– Sometimes when a property change occurs some other bean
may wish to validate the change and reject it if it is inappropriate.
The properties that undergo this kind of checking are referred as
constrained properties.
– Constrained Property set methods are required to support the
PropertyVetoException. This documents to the users of the
constrained property that attempted updates may be vetoed.
– The VetoableChangeListener event listener interface is used to
report updates to constrained properties.
– If a bean supports constrained properties then it should support
a normal pair of multicast event listener registration methods for
VetoableChangeListeners:
public void addVetoableChangeListener (VetoableChangeListener x);
public void removeVetoableChangeListener(VetoableChangeListener x);
9
Properties
Constrained Properties contd…
– When a property change occurs on a constrained property the
bean should call the VetoableChangeListener.vetoableChange
method on any registered listeners, passing a
PropertyChangeEvent object that encapsulates the locale-
independent name of the property and its old and new values.
– If the event recipient does not wish the requested edit to be
performed it may throw a PropertyVetoException.
– It is the source bean’s responsibility to catch this exception,
revert to the old value, and issue a new
VetoableChangeListener.vetoableChange event to report the
reversion.
10
Properties
Constrained Properties contd…
– For programming convenience a utility class
VetoableChangeSupport is provided that can be used to keep track
of VetoableChangeListeners and to fire VetoableChange events.
11
Events
Events:
– One of the core features of the Java Beans architecture.
– Provide a convenient mechanism for allowing components to be
plugged together in an application builder by allowing some
components to act as sources for event notifications that can
then be caught and processed by either scripting environments
or by other components.
Events overview:
– Event notifications are propagated from sources to listeners by
Java method invocations on the target listener objects.
– Each distinct kind of event notification is defined as a distinct
Java method. These methods are then grouped in EventListener
interfaces that inherit from java.util.EventListener.
12
Events
– Event listener classes identify themselves as interested in a
particular set of events by implementing some set of
EventListeners interfaces.
– The state associated with an event notification is normally
encapsulated in an event state object that inherits from
java.util.EventObject and which is passed as the sole argument
to the event method.
– Event sources identify themselves as sourcing particular events
by defining registration methods that conform to a specific
design pattern and accept (?) references to instances of
particular EventListener interfaces.
– In circumstances where listeners cannot directly implement a
particular interface, or when some additional behavior is
required, an instance of a custom adaptor class may be
interposed between a source and one or more listeners in order
to establish the relationship or to augment behavior.
13
Events
(Examples – code segment)
14
Events
(Examples – code segment)
Now,
public synchronized void addActionListener (ActionListener 1) {
listeners.addElement(1);
}
15