eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak


Introduction to Web Programming
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);

Previous Page | Next Page