_Java Q&A_
by Cliff Berg

Listing One
public class PasswordEventObject extends java.util.EventObject
{
    public PasswordEventObject(Object source, int id)
    {
        super(source);
        this.id = id;
    }
    public static final int PASSWORD_PROFFERED = 1;
    public static final int PASSWORD_APPROVED = 2;
    public static final int PASSWORD_REJECTED = 3;
    public int id;
    public String userid;
    public String password;
}


Listing Two
public interface PasswordListener
{
    public void handlePasswordEvent(PasswordEventObject peo);
}

Listing Three
public void add<listenerInterfaceType>(<listenerInterfaceType> arg)
public void remove<listenerInterfaceType>(<listenerInterfaceType> arg)


Listing Four
public void handlePasswordEvent(PasswordEventObject peo)
{
    if (peo.id != PasswordEventObject.PASSWORD_APPROVED) return;
    // Close this dialog
    System.out.println("Password verified; banana mochas all around!");
   dispose();
}


Listing Five
public void actionPerformed(java.awt.event.ActionEvent e)
{
    if (e.getSource() == okButton)
    {
        // Generate a PASSWORD_PROFFERED event
        PasswordEventObject peo = new PasswordEventObject(this, 
            PasswordEventObject.PASSWORD_PROFFERED);
        peo.userid = useridField.getText();
        peo.password = passwordField.getText();
        // Notify each listener
        java.util.Vector listeners;
        synchronized (this)
        {
            listeners = (java.util.Vector)(passwordListeners.clone());
        }
        for (int i = 0; i < listeners.size(); i++)
        {
            // Invoke the listener's handler
            PasswordListener pl = (PasswordListener)(listeners.elementAt(i));
            pl.handlePasswordEvent(peo);
        }
    }
    else if (e.getSource() == cancelButton)
    {
        dispose();
    }
}


Listing Six
class PasswordBeanAdapter // BeanBox generates and compiles this dynamically
{
    public void setTarget(PasswordListener t) { target = t; }
    public void addPasswordListener(...) ...
    public void removePasswordListener(...) ...etc.
}


Listing Seven
PasswordBeanAdapter pbAdapter = new PasswordBeanAdapter();
pbAdapter.setTarget(demoBean);      // link the adapter to the listener
pb.addPasswordListener(pbAdapter);  // and the source to the adapter 


Listing Eight
pb.addPasswordListener(demoBean);
demoBean.addPasswordListener(pb);

