Transcript 13_Beans

Java Beans - Basics
CIS 421
Web-based Java Programming
Very Simple Java Bean
(Example 1 – Inherited Property)
import java.awt.*;
public class VerySimpleBean extends Canvas {
//constructor to set inherited properties
public VerySimpleBean()
{
setSize(60,40);
setBackground(Color.red);
} //end of the constructor VerySimpleBean
} // end of the class VerySimpleBean
2
Creating a Very Simple Java Bean
 Compile the source code for the Bean.
– Javac VerySimpleBean.java
 Create a manifest file.
Name: VerySimpleBean.class
Java-Bean: True
 Create a JAR file.
– jar cfm VerySimpleBean.jar VerySimpleBean.mf
VerySimpleBean.class
 Load the jar file in the builder (e.g. beanbox).
– Now we can use it (but it doesn’t do anything)
3
Properties
 Simple Properties:
– Used to represent single attribute,
– Single value at an instance of time
4
Java Bean
(Example 2 – Simple Property: Color)
import java.awt.*;
import java.io.Serializable;
public class SimpleBean extends Canvas implements Serializable
{
private Color color = Color.green;
//Constructor: sets inherited properties
public SimpleBean()
{
setSize(60,40);
setBackground(Color.red);
}
//set method
public void setColor(Color newColor)
{
color = newColor;
repaint();
}
//get method
public Color getColor()
{
return color;
}
//override paint method
public void paint (Graphics g)
{
g.setColor(color);
g.fillRect(20,5,20,30);
}
}
5
Java Bean
(Example 3 – Simple Properties: Color & Label)
import java.awt.*;
public class SimpleBean1 extends Canvas
{
private Color color = Color.green;
private String label;
//Constructor: sets inherited properties
public SimpleBean1()
{ setSize(60,40);
setBackground(Color.red);
this.label="Bean";
setFont(new Font("Dialog", Font.PLAIN,12));
}
}
//set method
public void setColor(Color newColor)
{ color = newColor;
repaint();
}
//get method
public Color getColor()
{ return color; }
//set method for label
public void setLabel (String newLabel)
{ String oldLabel=label;
label=newLabel;
}
//get method for label
public String getLabel()
{ return label; }
//override paint method
public void paint (Graphics g)
{ g.setColor(color);
g.fillRect(20,5,20,30);
g.fillArc(5, 5, 30, 30, 0, 360);
g.fillArc(25, 5, 30, 30, 0, 360);
g.setColor(Color.blue);
int width = size().width;
int height = size().height;
FontMetrics fm = g.getFontMetrics();
g.drawString(label,
(width - fm.stringWidth(label)) / 2,
(height + fm.getMaxAscent()
- fm.getMaxDescent()) / 2);
}
} // 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);
– When a property change occurs on a bound property the bean
should call the PropertyChangeListener.propertyChange method
on any registered listeners, passing a PropertyChangeEvent
object that encapsulates the locale-independent name of the
property and its old and new values.
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 localeindependent 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.
Example – Bound and Constrained Property
JellyBean.java
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)
 Registering event listeners:
– Consider a typical button Bean that generates events when
pressed.
 If the button bean wants to be event source, it must provide two
methods that can be called by interested objects. One method adds
the caller to the list of listeners who are notified when the event
occurs. The other method removes the caller from the list of
interested listeners.
 public synchronized void addActionListener (ActionListener 1) { … }
 public synchronized void removeActionListener (ActionListener 1) { … }
 Action can be something like: Mouse, MouseMotion, etc.
addMouseListener ( … ) { … }, addMouseMotionListener ( … ) { … }
14
Events
(Examples – code segment)
 Button Bean contd…
– The button bean maintains a list (or Vector) of listeners. Thus the
source bean declares the following line:
 private Vector listeners = new Vector();
Now,
public synchronized void addActionListener (ActionListener 1) {
listeners.addElement(1);
}
public synchronized void removeActionListener(ActionListener 1) {
listeners.removeElement(1);
}
15