Java Beans and the New Events Model
by Eric Giguere


Listing One
public class SimpleBean {

    // Define the 'Visible' property
    public boolean getVisible() { return _visible; }
    public void setVisible( boolean visible ) { _visible = visible; }
    private boolean _visible;
}

Listing Two
(a) 
// A simple (but not a Beans) listener
interface ClickListener {
    void click();
}

(b)
// An implementation of a simple listener

public class MyClickListener implements ClickListener {
    public void click() {
        System.out.println( "You clicked!" );
    }
}

(c)
public class MyComponent {
    private Vector _clickListeners = new Vector();

    public void addClickListener( ClickListener listener ) {
        _clickListeners.addElement( listener );        
    }
    protected void handleClick() {
        for( int i = 0; i < _clickListeners.size(); ++i ){
            ((ClickListener) _clickListeners.elementAt(i)).click();
        }
    }
}

Listing Three
(a)
// A Beans listener interface
interface ClickListener extends java.util.EventListener {
    void click( ClickEvent event );
}

(b)
// A Beans event data class
public class ClickEvent extends java.util.EventObject {
    public ClickEvent( Object source ) {
        super( source );
    }
}

(c)
// A Beans event source
public class MyComponent {
    private Vector _clickListeners = new Vector();
    public synchronized void addClickListener( ClickListener listener ) {
        _clickListeners.addElement( listener );        
    }
    public synchronized void removeClickListener( ClickListener listener ) {
        _clickListeners.removeElement( listener );
    }
    protected void handleClick( ClickEvent event ) {
        Vector l;
        synchronized( this ) {
            l = (Vector) _clickListeners.clone();
        }
        for( int i = 0; i < l.size(); ++i ){
            ((ClickListener) l.elementAt(i)).click( event );
    }
}


Listing Four
(a)
public class MyDialog extends java.awt.Dialog implements ClickListener {
    public MyDialog() {
        super( null, true );
        pb = new PushButton( "pb_1" );
        pb.addClickListener( this );

        add( "Center", pb );
        pack();
    }
    public void click( ClickEvent event ) {
        System.out.println( "You clicked!" );
    }
    private PushButton pb;
    public static void main( String args[] ) {
        MyDialog app = new MyDialog();
        app.show();
    }
}

(b)
public class MyDialog2 extends java.awt.Dialog implements ClickListener {
    public MyDialog2() {
        super( null, true );
        pb_1 = new PushButton( "pb_1" );
        pb_1.addClickListener( this );
        pb_2 = new PushButton( "pb_2" );
        pb_2.addClickListener( this );

        add( "Center", pb_1 );
        add( "Center", pb_2 );
        pack();
    }
    public void click( ClickEvent event ) {
        Object source = event.getSource();
        if( source == pb_1 ){
            System.out.println( "You clicked pb_1!" );
        } else if( source == pb_2 ){
            System.out.println( "You clicked pb_2!" );
        }
    }
    private PushButton pb_1;
    private PushButton pb_2;

    public static void main( String args[] ) {
        MyDialog2 app = new MyDialog2();
        app.show();
    }
}

(c)
class pb_1_Relay implements ClickListener {
    public pb_1_Relay( MyDialog3 dlg ) { _target = dlg; }
    public void click( ClickEvent event ) {
        _target.pb_1_Click( event );
    }
    private MyDialog3 _target;
}
class pb_2_Relay implements ClickListener {
    public pb_2_Relay( MyDialog3 dlg ) { _target = dlg; }
    public void click( ClickEvent event ) {
        _target.pb_2_Click( event );
    }
    private MyDialog3 _target;
}
public class MyDialog3 extends java.awt.Dialog {
    public MyDialog3() {
        super( null, true );
        pb_1 = new PushButton( "pb_1" );
        pb_1.addClickListener( new pb_1_Relay( this ) );
        pb_2 = new PushButton( "pb_2" );
        pb_2.addClickListener( new pb_2_Relay( this ) );

        add( "Center", pb_1 );
        add( "South", pb_2 );
        pack();
    }
    public void pb_1_Click( ClickEvent event ) {
        System.out.println( "You clicked pb_1!" );
    }
    public void pb_2_Click( ClickEvent event ) {
        System.out.println( "You clicked pb_2!" );
    }
    private PushButton pb_1;
    private PushButton pb_2;
    public static void main( String args[] ) {
        MyDialog3 app = new MyDialog3();
        app.show();
    }
}

4


