Differences between listeners and adapters in swing

8

When programming graphical interfaces in Java using swing , we are always faced with both types, mainly to assign and create events from button actions or other components.

Although I've been working with swing for some time, I've always been curious about the difference between listeners and adapters, and how they are replaced in some cases like the example below:

  

Keyboard event in a JTextField with Adapter :

    jTextField1.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent e) {
            super.keyReleased(e);
        }
    });
  

Keyboard event in a JTextField with Listener :

    jTextField1.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyPressed(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyReleased(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });

The interface listener forces you to implement some methods, and the adapter only those needed.

Based on this, what differences (if any) between Listeners and Adapters within the swing API? How do I know when it's more advantageous to use one or the other?

    
asked by anonymous 19.02.2016 / 22:49

1 answer

5

In summary, adapter is a design pattern where a class implements an interface by overriding (dropping operations) the methods of the interface and you just override the methods that interest you, with the operations that interest you.

In listener , because it is an interface, you are forced to implement all methods, even those that do not interest you.

You can see details about the pattern Adapter here .

If you need / consume only one method (behavior) adapter is the best option. If you have problems with multiple inheritance, listener is your best option.

Update 21/02/2016

See the following example:

You want when the user clicks the window close X, a confirmation message is displayed to the user. Where he can give up closing the window.

For such a case, you should implement the WindowListener . Note that the interface defines 7 methods, but you are only interested in the windowClosing . Implemented the interface, you would have to implement the 7 methods. For this scenario, you can (or should) use an implementation of the WindowAdapter , which is abstract.

The class documentation says:

  

An abstract adapter class for receiving window events. The methods in this class are empty. This class exists as a convenience for creating listener objects.

That in free translation means:

  

An abstract adapter class to receive window events. The methods in this class are empty (no implementation). This class exists to create listeners for objects in an easier way.

In this situation, you would only override the method windowClosing of the WindowAdapter .

Note that the class WindowAdapter , implements 3 more interfaces, in addition to WindowListener , precisely with the intention of minimizing code. In other words, adaptar listeners.

I hope I have helped

    
19.02.2016 / 23:00