Chapter: Java The Complete Reference - The Java Library - Event Handling

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Adapter Classes - Java

Java provides a special feature, called an adapter class, that can simplify the creation of event handlers in certain situations.

Adapter Classes

 

Java provides a special feature, 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. 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, the MouseMotionAdapter class has two methods, mouseDragged( ) and mouseMoved( ), which are the methods defined by the MouseMotionListener interface. If you were interested in only mouse drag events, then you could simply extend MouseMotionAdapter and override mouseDragged( ). The empty implementation of mouseMoved( ) would handle the mouse motion events for you.

Table 24-4 lists several commonly used adapter classes in java.awt.event and notes the interface that each implements.

Adapter Class : Listener Interface

 

ComponentAdapter : ComponentListener

 ContainerAdapter : ContainerListener

 FocusAdapter : FocusListener

 KeyAdapter : KeyListener

 MouseAdapter : MouseListener and (as of JDK 6) MouseMotionListener and MouseWheelListener

 MouseMotionAdapter : MouseMotionListener

 WindowAdapter : WindowListener, WindowFocusListener, and WindowStateListener


Table 24-4   Commonly Used Listener Interfaces Implemented by Adapter Classes

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. AdapterDemo extends Applet. Its init( ) method creates an instance of MyMouseAdapter and registers that object to receive notifications of mouse events. It also creates an instance of MyMouseMotionAdapter and registers that object to receive notifications of mouse motion events. Both of the constructors take a reference to the applet as an argument.

 

MyMouseAdapter extends MouseAdapter and overrides the mouseClicked( ) method. The other mouse events are silently ignored by code inherited from the MouseAdapter class. MyMouseMotionAdapter extends MouseMotionAdapter and overrides the mouseDragged( ) method. The other mouse motion event is silently ignored by code inherited from the MouseMotionAdapter class. (MouseAdaptor also provides an empty implementation for MouseMotionListener. However, for the sake of illustration, this example handles each separately.)

 

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 the showStatus( ) method.

 

// Demonstrate an adapter.

import java.awt.*;

 

import java.awt.event.*; import java.applet.*; /*

 

<applet code="AdapterDemo" width=300 height=100> </applet>

 

*/

 

public class AdapterDemo extends Applet { public void init() {

 

addMouseListener(new MyMouseAdapter(this));

addMouseMotionListener(new MyMouseMotionAdapter(this));

}

 

}

class MyMouseAdapter extends MouseAdapter {

 

AdapterDemo adapterDemo;

 

public MyMouseAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo;

 

}

 

// Handle mouse clicked.

 

public void mouseClicked(MouseEvent me) { adapterDemo.showStatus("Mouse clicked");

}

 

}

 

class MyMouseMotionAdapter extends MouseMotionAdapter {

AdapterDemo adapterDemo;

 

public MyMouseMotionAdapter(AdapterDemo adapterDemo) {

this.adapterDemo = adapterDemo;

 

}

 

// Handle mouse dragged.

 

public void mouseDragged(MouseEvent me) {

adapterDemo.showStatus("Mouse dragged");

}

 

}

 

As you can see by looking at the program, not having to implement all of the methods defined by the MouseMotionListener and MouseListener interfaces saves you a considerable amount of effort and prevents your code from becoming cluttered with empty methods. As an exercise, you might want to try rewriting one of the keyboard input examples shown earlier so that it uses a KeyAdapter.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.