In the Java Swing library, adapter classes are a set of abstract classes that provide default implementations of listener interfaces. These adapter classes are best suited to use only the required methods to use by the developers. It does not force developers to override all the methods available in listener interfaces.
Adapter classes simplify the task of writing listener classes by providing default implementations for all the methods in an interface. This means that we can create a subclass of an adapter class and override only the methods that we are interested in implementing. This can save a lot of code, especially when we only need to handle a few of the events that a listener interface generates.
For example, the MouseAdapter class provides default implementations for all the methods of the MouseListener interface. If we want to handle only the mouse click event, we can extend the MouseAdapter class and override only the mouseClicked() method.
Following is an example of using a MouseAdapter class that handles the mouseClicked() event:
import java.awt.event.*;
public class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
// Handle the mouse click event here
}
}
// Create a component and add the listener
JComponent component = new JComponent();
component.addMouseListener(new MyMouseListener());
In this example, the MyMouseListener class extends the MouseAdapter
class and overrides the mouseClicked()
method to handle the mouse click event. Then, the listener is added to a JComponent using the addMouseListener()
method.
There are many adapter classes. Some of them are given below:
- WindowAdapter – Provides default implementations of the WindowListener interface methods for handling window events.
- KeyAdapter – Provides default implementations of the KeyListener interface methods for handling keyboard events.
- FocusAdapter – Provides default implementations of the FocusListener interface methods for handling focus events.
- ComponentAdapter – Provides default implementations of the ComponentListener interface methods for handling component events.
- ContainerAdapter – Provides default implementations of the ContainerListener interface methods for handling container events.
- MouseMotionAdapter – Provides default implementations of the MouseMotionListener interface methods for handling mouse motion events.
- MouseWheelAdapter – Provides default implementations of the MouseWheelListener interface methods for handling mouse wheel events.
- ItemAdapter – Provides default implementations of the ItemListener interface methods for handling item events.
- MenuAdapter – Provides default implementations of the MenuListener interface methods for handling menu events.
- PopupMenuAdapter – Provides default implementations of the PopupMenuListener interface methods for handling popup menu events.
These adapter classes can be used in the same way as the MouseAdapter example I provided earlier. They all provide default implementations of the interface methods, making it easier to create listener classes for specific events.