by Dinesh Thakur Category: Swing

The listener class that implements the Listener interface must provide bodies for all of the methods of that interface. It is not a problem for all the semantic listener interfaces such as ActionEvent, ItemEvent, TextEvent, AdapterEvent as each of them declares only one method. However, for all the low-level listener interfaces where each interface contains multiple methods, implementing each method can be somewhat tedious, especially when we have to define methods in which we are not interested. For example: Suppose we are interested in setting up only one listener interface method windowClosing() of the WindowListener interface that causes the program to terminate. In that case, we would not only need to provide code for windowClosing() method but also need to write empty bodies for the other methods available in the WindowListener interface.

class WindowEventFrame extends Frame implements WindowListener {
   ..................
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
publicvoid windowOpened(WindowEvent e){}
publicvoid windowClosed(WindowEvent e){}
publicvoid windowActivated(WindowEvent e){}
publicvoid windowDeactivated(WindowEvent e){}
publicvoid windowlconified(WindowEvent e){}
publicvoid windowDeiconified(WindowEvent e){}
..................
}

To avoid this unnecessary code, the java.awt.event package provides adapter classes for various event-listener types. The event listener interfaces that contain more than one method have a corresponding event adapter class that implements the interface and defines each method in the interface with an empty method body. For example, the adapter class for the WindowListener interface is WindowAdapter.

The following table lists some of the listener interfaces and their corresponding adapter classes.

listener interfaces and their corresponding adapter classes

Now instead of implementing an event listener interface, you can extend the corresponding event adapter class and define only those methods in which you are interested. For example, The following code segment shows that a class MyWindowAdapter extends WindowAdapter and implements the windowClosing() method.

class MyWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent e){
       System.exit(0);
    }
}

This class can be registered using the following statement,
this.addWindowListener(new MyWindowAdapter());
However, if the class is a subclass of a Frame or Applet class, then you cannot define the class as a subclass of WindowAdapter. In such a case, you can use inner classes. So the preceding MyWindowAdapter class and addWindowListener statement replaced with the following statements,

this.addWindowListener( new WindowAdapter() {
     public void windowClosing(WindowEvent e) {
         System.exit(0);
     }
}) ;

You can read this syntax as defining an anonymous inner class as a subclass ofWindowAdapter, create an instance of this inner class and use this instance as an argument to the addWindowListener () method.
The following program demonstrates how the adapter class is created and used.

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
class AdapterExample extends JFrame
{
       AdapterExample()
       {
          this.addWindowListener( new WindowAdapter() {
          public void windowClosing(WindowEvent e)
                           {
                              System.exit(0);
                           }
                           });
       }
}
    class AdapterClassJavaExample
{
      public static void main(String [] args)
      {
        AdapterExample frame = new AdapterExample();
        frame.setTitle("Adapter Class Java Example");
        frame.setBounds(100,200,200,200);
        frame.setVisible(true);
      }
}

Event Adapter Class in Java Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles