| 15.4 Custom EventsBeans can use the standard event
types defined in the java.awt.event and
javax.swing.event packages, but they
don't have to. Our YesNoPanel
class defines its own event type, AnswerEvent.
Defining a new event class is really quite simple;
AnswerEvent is shown in Example 15-4. Example 15-4. AnswerEvent.javapackage je3.beans;
/**
 * The YesNoPanel class fires an event of this type when the user clicks one
 * of its buttons.  The id field specifies which button the user pressed.
 **/
public class AnswerEvent extends java.util.EventObject {
    public static final int YES = 0, NO = 1, CANCEL = 2;  // Button constants
    protected int id;                             // Which button was pressed?
    public AnswerEvent(Object source, int id) {
        super(source);
        this.id = id;
    }
    public int getID( ) { return id; }             // Return the button
}Along with the
AnswerEvent class, YesNoPanel
also defines a new type of event listener interface,
AnswerListener, that defines the methods that must
be implemented by any object that wants to receive notification from
a YesNoPanel. The definition of
AnswerListener is shown in Example 15-5. Example 15-5. AnswerListener.javapackage je3.beans;
/**
 * Classes that want to be notified when the user clicks a button in a
 * YesNoPanel should implement this interface.  The method invoked depends
 * on which button the user clicked.
 **/
public interface AnswerListener extends java.util.EventListener {
    public void yes(AnswerEvent e);
    public void no(AnswerEvent e);
    public void cancel(AnswerEvent e);
} |