Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Adapter Classes


Java provides a special feature, called anadapter class,that can simplify the creation of event
handlers in certain situations. An adapter class provides an empty implementation of all
methods in an event listener interface. Adapter classes are useful when you want to receive
and process only some of the events that are handled by a particular event listener interface.
You can define a new class to act as an event listener by extending one of the adapter classes
and implementing only those events in which you are interested.
For example, theMouseMotionAdapterclass has two methods,mouseDragged( )
andmouseMoved( ), which are the methods defined by theMouseMotionListener
interface. If you were interested in onlymouse drag events, then you could simply extend
MouseMotionAdapterand overridemouseDragged( ). The empty implementation of
mouseMoved( )would handle the mouse motion events for you.
Table 22-4 lists the commonly used adapter classes injava.awt.eventand notes the
interface that each implements.
The following example demonstrates an adapter. It displays a message in the status bar
of an applet viewer or browser when the mouse is clicked or dragged. However, all other
mouse events are silently ignored. The program has three classes.AdapterDemoextends
Applet. Itsinit( )method creates an instance ofMyMouseAdapterand registers that object
to receive notifications of mouse events. It also creates an instance ofMyMouseMotionAdapter
and registers that object to receive notifications of mouse motion events. Both of the constructors
take a reference to the applet as an argument.
MyMouseAdapterextendsMouseAdapterand overrides themouseClicked( )method.
The other mouse events are silently ignored by code inherited from theMouseAdapter
class.MyMouseMotionAdapterextendsMouseMotionAdapterand overrides the
mouseDragged( )method. The other mouse motion event issilently ignored by code
inherited from theMouseMotionAdapterclass.
Note that both of the event listener classes save a reference to the applet. This information
is provided as an argument to their constructors and is used later to invoke theshowStatus( )
method.

// Demonstrate an adapter.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*

Chapter 22: Event Handling 659


Adapter Class Listener Interface
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
FocusAdapter FocusListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
WindowAdapter WindowListener

TABLE 22-4

Commonly Used
Listener Inter faces
Implemented by
Adapter Classes
Free download pdf