from Sun's color annotated
code samples of the sample beans
P.K.
package sunw.demo.buttons;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.Serializable;
import java.util.Vector;
/**
* A simple Java Beans button. OurButton is a "from-scratch"
* GUI component that's derived from Canvas. It's a good
example of
* how to implement bound properties and support for event listeners.
* Parts of the source are derived from sun.awt.tint.TinyButtonPeer.
*/
public class OurButton extends Canvas implements Serializable,
MouseListener, MouseMotionListener {
// Methods for registering/deregistering event listeners
/**
* The specified ActionListeners actionPerformed
method will
* be called each time the button is clicked.
The ActionListener
* object is added to a list of ActionListeners
managed by
* this button, it can be removed with removeActionListener.
* Note: the JavaBeans specification does not
require ActionListeners
* to run in any particular order.
*
* @see #removeActionListener
* @param l the ActionListener
*/
public synchronized void addActionListener(ActionListener
l) {
pushListeners.addElement(l);
}
/**
* Remove this ActionListener from the buttons
internal list. If the
* ActionListener isn't on the list, silently
do nothing.
*
* @see #addActionListener
* @param l the ActionListener
*/
public synchronized void removeActionListener(ActionListener
l) {
pushListeners.removeElement(l);
}
/**
* The specified PropertyChangeListeners propertyChange
method will
* be called each time the value of any bound
property is changed.
* The PropertyListener object is addded to
a list of PropertyChangeListeners
* managed by this button, it can be removed
with removePropertyChangeListener.
* Note: the JavaBeans specification does not
require PropertyChangeListeners
* to run in any particular order.
*
* @see #removePropertyChangeListener
* @param l the PropertyChangeListener
*/
public void addPropertyChangeListener(PropertyChangeListener
l) {
changes.addPropertyChangeListener(l);
}
/**
* Remove this PropertyChangeListener from
the buttons internal list.
* If the PropertyChangeListener isn't on the
list, silently do nothing.
*
* @see #addPropertyChangeListener
* @param l the PropertyChangeListener
*/
public void removePropertyChangeListener(PropertyChangeListener
l) {
changes.removePropertyChangeListener(l);
}
//----------------------------------------------------------------------
/**
* This method has the same effect as pressing
the button.
*
* @see #addActionListener
*/
public void fireAction() {
if (debug) {
System.err.println("Button " + getLabel() + " pressed.");
}
Vector targets;
synchronized (this) {
targets = (Vector) pushListeners.clone( );
}
ActionEvent actionEvt =
new ActionEvent(this, 0, null);
for (int i = 0; i < targets.size();
i++) {
ActionListener target = (ActionListener)targets.elementAt(i);
target.actionPerformed(actionEvt);
}
}