Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Java AWT and Swing, a listener is an interface that defines callbacks for events, while an adapter is a convenience class with empty implementations of a multi-method listener interface. Implement a listener directly when you need its methods; extend an adapter when you need only a few of them.

What listeners and adapters do

An event source—such as a Swing component—can notify registered objects when something happens. A listener supplies the callback methods the source can invoke. Registration commonly uses an add…Listener() method, and removal uses the corresponding remove…Listener() method.

An adapter does not replace the event source or registration mechanism. It is a class that implements a listener interface by providing empty versions of its methods. You subclass it and override the callback or callbacks you want to handle. The Java SE 24 java.awt.event package describes event-listener adapters as convenience classes for writing event listeners.

When to use each

Situation Prefer Why
The listener interface has one callback, such as ActionListener. Implement the listener directly There are no extra methods to fill with empty implementations, and Java provides no corresponding adapter for ActionListener.
The interface has several callbacks, but you need only one or two. Extend its adapter Inherited empty methods handle the callbacks you do not care about.
Your class already extends another class. Implement the listener, or use an inner class that extends the adapter Java classes can extend only one superclass, so your class cannot extend both the application superclass and the adapter.
You need independent handlers or need to control their lifetimes separately. Register separate listener objects Each handler can have its own behavior, but you must keep track of and remove listeners when they are no longer needed.

MouseListener example: adapter versus direct implementation

For a click-only handler, an anonymous MouseAdapter lets you override just mouseClicked:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
component.addMouseListener(new MouseAdapter() {
    @Override public void mouseClicked(MouseEvent e) {
        // Handle the click.
    }
});

Implementing MouseListener directly requires all of its callbacks, including methods your code may not use:

component.addMouseListener(new MouseListener() {
    @Override public void mouseClicked(MouseEvent e) { }
    @Override public void mousePressed(MouseEvent e) { }
    @Override public void mouseReleased(MouseEvent e) { }
    @Override public void mouseEntered(MouseEvent e) { }
    @Override public void mouseExited(MouseEvent e) { }
});

Both forms register a mouse listener. The adapter version mainly avoids repetitive empty method bodies; it does not change which events the component can report.

Which listener interfaces have adapters?

Common AWT pairings include MouseListener with MouseAdapter, KeyListener with KeyAdapter, ComponentListener with ComponentAdapter, ContainerListener with ContainerAdapter, and MouseInputListener with MouseInputAdapter. These are useful when an interface has multiple callbacks and a class needs only a subset.

Not every listener has an adapter. For single-method interfaces such as ActionListener, ItemListener, and ChangeListener, there is no set of unused interface methods for an adapter to fill in. Implement the interface directly, or use a lambda where the interface is functional.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Inheritance, readability, and handler lifecycle

Account for Java’s single inheritance

Because an adapter is a class, extending it uses the class’s one superclass slot. If your main class must extend another class, implement the listener interface directly or put the adapter subclass in an inner class. The inner class can extend the adapter while the outer class keeps its own superclass.

Keep callbacks quick

Oracle’s event-listener guidance emphasizes that listeners should execute quickly. A lengthy callback can delay the interface’s response; keep event handling brief and move slow work out of the callback when appropriate.

Remove listeners when their lifetime ends

Event sources and listeners can hold references to one another. In long-lived applications or dynamically created components, remove a listener when its owner or source is being disposed of. Retain the listener object if you will need to pass that same object to the matching remove…Listener() method later.

The OSGi Alliance’s 2019 white paper reported more than 130 event, adapter, and listener classes in Java. That is a dated ecosystem figure, not a current count; the practical choice remains specific to the listener interface and the objects’ lifetimes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick decision

  • One callback to implement: use the listener interface directly.
  • Several callbacks, only a few relevant: extend the adapter.
  • Already extending another class: implement the listener or use an inner adapter subclass.
  • Handler objects can outlive their usefulness: plan their removal when registering them.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.