Java adapter classes provide the default implementation of listener interfaces. If you inherit the adapter class, you will not be forced to provide the implementation of all the methods of listener interfaces. So it saves code.
Pros of using Adapter classes:
It assists the unrelated classes to work combinedly.
It provides ways to use classes in different ways.
It increases the transparency of classes.
It provides a way to include related patterns in the class.
It provides a pluggable kit for developing an application.
It increases the reusability of the class.
The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages. The Adapter classes with their corresponding listener interfaces are given below.
java.awt.event Adapter classes
Adapter class | Listener interface |
---|---|
WindowAdapter | WindowListener |
KeyAdapter | KeyListener |
MouseAdapter | MouseListener |
MouseMotionAdapter | MouseMotionListener |
FocusAdapter | FocusListener |
ComponentAdapter | ComponentListener |
ContainerAdapter | ContainerListener |
HierarchyBoundsAdapter | HierarchyBoundsListener |
java.awt.dnd Adapter classes
Adapter class | Listener interface |
---|---|
DragSourceAdapter | DragSourceListener |
DragTargetAdapter | DragTargetListener |
javax.swing.event Adapter classes
Adapter class | Listener interface |
---|---|
MouseInputAdapter | MouseInputListener |
InternalFrameAdapter | InternalFrameListener |
Java WindowAdapter Example
In the following example, we are implementing the WindowAdapter class of AWT and one its methods windowClosing() to close the frame window.
AdapterExample.java
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
public class AdapterExample {
// object of Frame
Frame f;
// class constructor
AdapterExample() {
// creating a frame with the title
f = new Frame ("Window Adapter");
// adding the WindowListener to the frame
// overriding the windowClosing() method
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
f.dispose();
}
});
// setting the size, layout and
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
// main method
public static void main(String[] args) {
new AdapterExample();
}
}
Output:

Java MouseAdapter Example
In the following example, we are implementing the MouseAdapter class. The MouseListener interface is added into the frame to listen the mouse event in the frame.
MouseAdapterExample.java
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the MouseAdapter class
public class MouseAdapterExample extends MouseAdapter {
// object of Frame class
Frame f;
// class constructor
MouseAdapterExample() {
// creating the frame with the title
f = new Frame ("Mouse Adapter");
// adding MouseListener to the Frame
f.addMouseListener(this);
// setting the size, layout and visibility of the frame
f.setSize (300, 300);
f.setLayout (null);
f.setVisible (true);
}
// overriding the mouseClicked() method of the MouseAdapter class
public void mouseClicked (MouseEvent e) {
// creating the Graphics object and fetching them from the Frame object using getGraphics() method
Graphics g = f.getGraphics();
// setting the color of graphics object
g.setColor (Color.BLUE);
// setting the shape of graphics object
g.fillOval (e.getX(), e.getY(), 30, 30);
}
// main method
public static void main(String[] args) {
new MouseAdapterExample();
}
}
Output:

Java MouseMotionAdapter Example
In the following example, we are implementing the MouseMotionAdapter class and its different methods to listen to the mouse motion events in the Frame window.
MouseMotionAdapterExample.java
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the MouseMotionAdapter class
public class MouseMotionAdapterExample extends MouseMotionAdapter {
// object of Frame class
Frame f;
// class constructor
MouseMotionAdapterExample() {
// creating the frame with the title
f = new Frame ("Mouse Motion Adapter");
// adding MouseMotionListener to the Frame
f.addMouseMotionListener (this);
// setting the size, layout and visibility of the frame
f.setSize (300, 300);
f.setLayout (null);
f.setVisible (true);
}
// overriding the mouseDragged() method
public void mouseDragged (MouseEvent e) {
// creating the Graphics object and fetching them from the Frame object using getGraphics() method
Graphics g = f.getGraphics();
// setting the color of graphics object
g.setColor (Color.ORANGE);
// setting the shape of graphics object
g.fillOval (e.getX(), e.getY(), 20, 20);
}
public static void main(String[] args) {
new MouseMotionAdapterExample();
}
}
Output:

Java KeyAdapter Example
In the following example, we are implementing the KeyAdapter class and its method.
KeyAdapterExample.java
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the KeyAdapter class
public class KeyAdapterExample extends KeyAdapter {
// creating objects of Label, TextArea and Frame
Label l;
TextArea area;
Frame f;
// class constructor
KeyAdapterExample() {
// creating the Frame with title
f = new Frame ("Key Adapter");
// creating the Label
l = new Label();
// setting the location of label
l.setBounds (20, 50, 200, 20);
// creating the text area
area = new TextArea();
// setting the location of text area
area.setBounds (20, 80, 300, 300);
// adding KeyListener to text area
area.addKeyListener(this);
// adding the label and text area to frame
f.add(l);
f.add(area);
// setting the size, layout and visibility of frame
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
// overriding the keyReleased() method
public void keyReleased (KeyEvent e) {
// creating the String object to get the text fromTextArea
String text = area.getText();
// splitting the String into words
String words[] = text.split ("\\s");
// setting the label text to print the number of words and characters of given string
l.setText ("Words: " + words.length + " Characters:" + text.length());
}
// main method
public static void main(String[] args) {
new KeyAdapterExample();
}
}
Output:

If you like this answer, you can give me a coffee by click here (view Ads)
Leave a Review