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. |