Event Processing

There are several mechanisms for handling external events such as user input.

Event Listeners

The most generic means of handling external events is through the event listener mechanism. To associate an event handler with a user interface object such as a button, you pass an object of a class that implements the proper listener inter- face (such as ActionListener) to a method addXListener(), where X is the type of event (such as Action).

For example, the following code handles a button click:

import java.awt.*
import java.awt.event.*;
class TV
{
 Button button = new Button();
 TV()
 {
 // add a MyActionListener to the button defined above
 button.addActionListener(new MyActionListener());
 }
 /**
 * Handle the button press event.
 */
 class MyActionListener implements ActionListener
 {
 public void actionPerformed(ActionEvent ae)
 {
 // …handle the button pressed…
 }
 }
}


The inner class MyActionListener implements the ActionListener interface with the method actionPerformed(). The method addActionListener() adds the newly created MyActionListener object to the list of objects to receive the action event. The meaning of "action event" is specific to the user interface object, but for a button an action event equates to a user click.

Delegates

WFC objects use an approach to event processing that doesn't require the creation of separate classes.

NOTE
The delegate mechanism is based on the delegate keyword, which is a Microsoft Visual J++ 6 extension to the Java language.

The delegate approach to event processing is based on classes created by the delegate keyword. In the following example, the class KeyPressEventHandler is a delegate class designed to handle keyPress events.

import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
class TV
{
 Text text = new Text();
 TV()
 {
 text.addOnKeyPress(
 new KeyPressEventHandler(this.text_keyPress)
 );
 }
 private void text_keyPress(Object source, KeyPressEvent e)
 {
 // …process the keypress event…
 }
}


The KeyPressEventHandler() class is created with a reference to the method text_keyPress() and to the current object. Comments