Other Methods for Handling Events 

  • As we have shown, you can catch all the events you are interested in by implementing a handleEvent() method. When you use handleEvent() to catch events, you need to perform a test to check for the event ID that you're looking for. In our example, to check for an action event we performed the following test: 
  • if (event.id == Event.ACTION_EVENT)
  • However, if you want to avoid these types of comparisons in your code, there are other methods you can implement instead of handleEvent(). Unlike handleEvent(), these other methods are tied to specific event types and can only be called when their particular event type is generated. 
  • The action() method, for example, is called only when an action event is generated and not when events of other types occur. Thus, instead of implementing a handleEvent() routine and checking for an event ID of ACTION_EVENT, you can simply implement an action() method. 
  • As an example, the following code performs the exact same function as our previous code to handle an action event in a button. However, in this code we handle the event by implementing an action() routine instead of a handleEvent() routine: 
  • class MyButton extends Button
    {
    public boolean action(Event event, Object arg)
        {
        System.out.println("I got an action: " + event);
        return super.action(event, arg);
        }
    }
  • Notice that we didn't have to check the event ID. It's common to see action events handled in an action() routine instead of a handleEvent() routine. Be aware, however, that they can be handled in either method. 
When does the action() method get called? When the action event is passed up in handleEvent() through the chain of superclasses, it eventually reaches Component. In particular, the action event eventually reaches the handleEvent() method in the Component class. The handleEvent() routine in the Component class calls the action() method on the component if the event is an action event. By default, the action method of a component does nothing. However, if you have provided an action() event as shown above, you will receive the event.
  • action() events aren't the only events that are handled in other methods. The following table shows the other methods you can implement for their respective event IDs. 
Event ID Method
ACTION_EVENT public boolean action(Event e, Object arg);
GOT_FOCUS public boolean gotFocus(Event e, Object arg);
KEY_PRESS public boolean keyDown(Event e, int key);
KEY_RELEASE public boolean keyUp(Event e, int key);
LOST_FOCUS public boolean lostFocus(Event e, Object arg);
MOUSE_DOWN public boolean mouseDown(Event e, int x, int y);
MOUSE_DRAG public boolean mouseDrag(Event e, int x, int y);
MOUSE_ENTER public boolean mouseEnter(Event e, int x, int y);
MOUSE_EXIT public boolean mouseExit(Event e, int x, int y);
MOUSE_MOVE public boolean mouseMove(Event e, int x, int y);
MOUSE_UP public boolean mouseUp(Event e, int x, int y);

Additional Resources:

Event Types
Table of Contents
Event Propagation 


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

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