Java In A Nutshell

Chapter 10 - Java Beans

Contents:
Bean Basics
A Simple Bean
A More Complex Bean
Custom Events
Specifying Bean Information
Defining a Simple Property Editor
Defining a Complex Property Editor
Defining a Bean Customizer
Naming Patterns and Conventions


10.4 Custom Events

Beans can use the standard AWT event types defined in the java.awt.event package, but they do not have to. Our YesNoDialog class defines its own event type, AnswerEvent. Defining a new event class is really quite simple; AnswerEvent is shown in Example 10.3.

Example 10.3: The AnswerEvent Class

package oreilly.beans.yesno;
public class AnswerEvent extends java.util.EventObject {
  protected int id;
  public static final int YES = 0, NO = 1, CANCEL = 2;
  public AnswerEvent(Object source, int id) {
    super(source);
    this.id = id;
  }
  public int getID() { return id; }
}

Along with the AnswerEvent class, YesNoDialog also defines a new type of event listener interface, ActionListener, that defines the methods that must be implemented by any object that wants to receive notification from a YesNoDialog. The definition of AnswerListener is shown in Example 10.4.

Example 10.4: The AnswerListener Interface

package oreilly.beans.yesno;
public interface AnswerListener extends java.util.EventListener {
  public void yes(AnswerEvent e);
  public void no(AnswerEvent e);
  public void cancel(AnswerEvent e);
}




JavaTM is a trademark of Sun Microsystems, Inc.
Other companies, products, and service names may be trademarks or service marks of others.

Copyright  Trademark



 Java EducationJava Home 
IBM HomeOrderEmployment