Ask Questions?

View Latest Questions


 
 

Listeners
Posted on: July 26, 2006 at 12:00 AM
you will add a listener to it.

Java Notes

Listeners

A listener is called when the user does something to the user interface that causes an event. Altho these events usually come from the user interface, they can have other sources (eg, a Timer).

Button listener example

After a button is created, you will add a listener to it. Eg,

   b.addActionListener(listener_object);

When the button is clicked, a call is made to the actionPerformed() method defined in the class of the listener object. An ActionEvent object is passed as a parameter to it actionPerformed().

The listener method must be called actionPerformed

There is (unfortunately) no way to use any given method as a listener -- the listener must be called actionPerformed.

One per class. Because there can only be one actionPerformed method in a class, a new class is needed for every separate listener, or you have to share an actionPerformed method and use the ugly technique of figuring out who caused the call.

Incidental note. To address this awkward situation C# has delegates, which allow any method to be a listener, provided it has the correct return type and parameters. This doesn't provide any additional functionality, but it is more convenient.

You need to create an instance of the class which defines the actionPerformed method for the button, and it is this instance that becomes the button listener.

Common Listener Strategies

Here are common ways to write listeners:

  1. Named Inner Class Listeners are one of the most common ways to write small programs. They are convenient to write and and be shared with several components.
  2. Anonymous Inner Class Listeners are sometimes used to associate a different listener with each button or other control. This is a little quicker to write, but lacks some of the flexibility of inner class listeners.
  3. Top-level Listeners (this) are commonly used where there is only one source of an event.