O'Reilly logo

Developing Java Beans by Robert Englander

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Event Adapters in the AWT Package

The java.awt.event package provides adapters for various event-listener types. These adapters are nothing more than empty shells that implement the methods in the various listener interfaces. These adapters don’t provide much added value, but you can subclass them to build basic demultiplexing adapters. They relieve you from having to implement every interface method yourself, although the burden is quite small.

Let’s look at an example. The following chunk of code is the listing for the java.awt.event.FocusAdapter class:

public class FocusAdapter implements FocusListener
{
   public FocusAdapter()
   {
   }

   public void focusGained(FocusEvent e)
   {
   }

   public void focusLost(FocusEvent e)
   {
   }
}

So, as you can see, these adapters aren’t all that helpful. You’ll find an adapter class in java.awt.event for every event-listener interface that has more than one method in it. I think the fact that these adapters are provided at all is an indication that the technique is expected to be widely used. But I expect that more complex adapters will find more use than these.

A Generic Button Adapter

It’s common for a dialog box or other window to contain multiple Button objects—a perfect place to make use of an intelligent adapter. The GenericTemperatureAdapter class shown earlier can be easily modified for this purpose. Let’s create an adapter that will handle the ActionListener interface for multiple buttons, and will forward the ActionEvent to a method on the target object. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required