Listening For Events with the 1.0 Model 

  • For instance, in our OK button example, when the button generates an action event, its own handleEvent() method is first called with the action event information. 
  • Therefore, we can subclass the button and implement a handleEvent() routine to look for action events. Consider the following example: 
  • class MyButton extends Button
    {
    public boolean handleEvent(Event event)
      {
      if (event.id == Event.ACTION_EVENT)  
        System.out.println("I got an action: " + 
                           event);
        return super.handleEvent(event);
      }
    }
  • In this case, we simply create a subclass of Button which prints out a note whenever it is pressed.
Notice that we called super.handleEvent() in the last line of code in our handleEvent() implementation. It's very important to do this, since this passes the event to the button's superclass (in this case Button) to allow it the option of also handling events. 

The superclass of MyButton is Button--the button class itself. The Button class listens for mouse up and down events in its handleEvent() routine. When a mouse up and down occurs in the button, the Button posts an action event. If we didn't call super.handleEvent() in our MyButton class, the Button class's handleEvent() method would never get called and would never know that the mouse had been pressed and released. Since it wouldn't get called, it would never generate an action event. So, our MyButton class would never receive an action event, since one would never be generated. 

That's why its important to call super.handleEvent() at the end of a handleEvent() implementation. You need to make sure the object's superclasses also process the event. In this case, if we didn't call super.handleEvent(), the button wouldn't respond to anything at all since our routine would be catching all the events and never sending them on to its superclass. 

Some books state that you should return true in your handleEvent() method if you are handling the event yourself and don't want to pass on the event to other possibly interested parties. However, we would simply suggest you always call super.handleEvent() to pass the event on to the class's superclass as well as other possibly interested parties.

Additional Resources:

How Events Originate 
Table of Contents
JDK 1.0 Event Handling


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory