• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Swing » Java Adapter Classes with Example
Next →
← Prev

Java Adapter Classes with Example

By Dinesh Thakur

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

You’ll also like:

  1. Anonymous Inner Classes in Java with Examples
  2. What is Graphics Adapter?
  3. What is NICs (Network Adapter)
  4. What is CGA (Color Graphics Adapter)?
  5. What is EGA (enhanced graphics adapter)?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW