Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


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


Up to => Home / Authoring / Scripting / Tutorial




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers