Core Java - [Java Adapter Classes]

♠ Posted by Unknown in at 10:52

Java Adapter Classes


Java provides a special features, called an adapter 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. 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, the "MouseMotionAdapter" class has two methods, mouseDragged() and mouseMoved(). The signatures of these empty methods are exactly as defined in the "MouseMotionListener" interface. If you were interested in only mouse drag events, then you could simply extend "MouseMotionAdapter" and implement mouseDragged(). The empty implementation of mouseMoved() would handle the mouse motion events for you.

Adapter Class                         Listener Interface

ComponentAdapter                ComponentListener
ContainerAdapter                   ContainerListener
FocusAdapter                         FocusListener
KeyAdapter                            KeyListener
MouseAdapter                        MouseListener
MouseMotionAdapter              MouseMotionListener
WindowAdapter                     WindowListener

0 comments:

Post a Comment