Adapter Class In Java

Introduction

This article introduces the Adapter class in Java.

What the Adapter class is

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 siince 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.

Now understand various methods of the WindowListener class

It contains the following public methods:

  • public void windowActivated(WindowEvent we)

          This method is used when the current Window is set to be activated.

  • public void windowClosed(WindowEvent we)

          Used when a window is closed.

  • public void windowClosing(WindowEvent we)

          When a user wants to close a window this method is used.

  • public void windowDeactivated(WindowEvent we)

          Used when the current window is not activated for a long time.

  • public void windowDeiconfied(WindowEvent we)

          Used when a window changes its position from minimized to normal (restored) state.

  • public void windowIconified(WindowEvent we)

          Used when a window is changed from normal to minimized state.

  • public void windowIconified(WindowEvent we)

          It makes the window visible.

Create an adapter

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

fig-1.jpg

It generates the following frame:

fig-2.jpg

Note: When we click on the "Close" link it is closed.

Up Next
    Ebook Download
    View all
    Learn
    View all