Java Event Handling

Adapter Classes for Listener Interfaces

 

Any class which handles events must implement a listener interface for the event type it wishes to handle.  A class can implement any number of listener interfaces.  To implement a listener interface the class must implement every method in the interface.  Sometimes you may want to implement only one or two of the methods in an interface.  You can avoid having to write your own implementation of all the methods in an interface by using an adapter class.  An adapter class is a class that already implements all the methods in its corresponding interface.  Your class must extend the adapter class by inheritance so you can extend only one adapter class in your class (Java does not support multiple inheritance).  Here are the Java adapter classes and the listener interfaces they implement.  Notice that adapters exist for only listener interfaces with more than one method.

 

 

 

          Adapter Class                                                        Listener Interface

 

          ComponentAdapter                                                 ComponentListener

 

          ContainerAdapter                                                    ContainerListener

 

          FocusAdapter                                                         FocusListener

 

          KeyAdapter                                                             KeyListener

 

          MouseAdapter                                                         MouseListener

 

          MouseMotionAdapter                                             MouseMotionListener

 

          WindowAdapter                                                      WindowListener