Introduction
This article introduces the Adapter class in Java.
What is Adapter class
The Adapter class provides the default modification of all methods of an interface; we don't need to modify all the methods of the interface so we can say it reduces coding burden. Sometimes or often we need a few methods of an interface. For that, the Adapter class is very helpful since it already modifies all the methods of an interface and by implementing the Adapter class, we only need to modify the required methods.
Advantages of the Adapter class
- Assists unrelated classes to work together.
- Provides a way to use classes in multiple ways.
- Increases the transparency of classes.
- Its provides a way to include related patterns in a class.
- It provides a pluggable kit for developing applications.
- It makes a class highly reusable.
Examples
The following examples contain the following Adapter classes:
- ContainerAdapter class.
- KeyAdapter class.
- FocusAdapter class.
- WindowAdapter class.
- MouseAdapter class.
- ComponentAdapter class.
- MouseMotionAdapter class.
- etcetera
Let's use a scenario that shows the need for the Adapter class
As we know, the WindowListener interface contains seven methods. Whenever we need to use this interface we must modify or implement all 7 methods. So when we use the Adapter class (in other words WindowAdapter) we don't need to modify all the methods, since all the methods are already modified in it. We only need to implement those methods that are modified.
Methods of the WindowListener class
It contains the following public methods:
The following method is used when the current Window is set to be activated.
- public void windowActivated(WindowEvent we)
The following method is used when a window is closed.
- public void windowClosed(WindowEvent we)
When a user wants to close a window the following method is used.
- public void windowClosing(WindowEvent we)
The following method is used when the current window is not activated for a long time.
- public void windowDeactivated(WindowEvent we)
The following method is used when a window changes its position from minimized to normal (restored) state.
- public void windowDeiconfied(WindowEvent we)
The following method is used when a window is changed from normal to minimized state.
- public void windowIconified(WindowEvent we)
The following method makes the window visible.
- public void windowIconified(WindowEvent we)
Create an adapter class
Create an adapter using:
- public class WindowAdapter implements WindowListner
- {
- public void windowClosed(WindowEvent e){}
- public void windowOpened(WindowEvent e){}
- public void windowIconified(WindowEvent e){}
- public void windowDeiconified(WindowEvent e){}
- public void windowActivated(WindowEvent e){}
- public void windowDeactivated(WindowEvent e){}
- public void windowClosing(WindowEvent e){}
- }
The following is a test program using an Adapter class.
WindowAdapterEx.java
In this example; we are using a close method (in other words windowClosing) of the Adapter class that shows the benefits of using the Adapter class.
- import java.awt.event.WindowAdapter;
- import java.awt.Frame;
- import java.awt.event.WindowEvent;
- public class WindowAdapterEx extends Frame
- {
- public WindowAdapterEx()
- {
- WindowAdapterClose clsme = new WindowAdapterClose();
- addWindowListener(clsme);
- setTitle("WindowAdapter frame closing");
- setSize(400, 400);
- setVisible(true);
- }
- public static void main(String args[])
- {
- new WindowAdapterEx();
- }
- }
- class WindowAdapterClose extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- }
Outputs
It generates the following frame:
Note: When we click on the "Close" link it is closed.