Differences between listeners and Adapters in swing

Asked

Viewed 604 times

7

When programming java graphical interfaces using swing, we always come across both types, mainly to assign and create events from button actions or other components.

Though I’ve been messing around with swing, I’ve always been curious about the difference between listeners and Adapters, and how they replace themselves 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 Listener interface requires implementing some methods, and Adapter only the necessary ones.

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

1 answer

6


In short, adapter is a project pattern where a class implements an interface nullifying (leaving no operations) the interface methods and you just override the methods that interest you, with the operations that interest you.

Already in the listener, for being an interface, you are required 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 multiple inheritance problems, the listener is your best option.

Update 21/02/2016

See the following example:

You want when the user clicks on the closing window 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 interface Windowlistener. Note that the interface defines 7 methods, but you are only interested in the method windowClosing. Once the interface is implemented, you would have to implement the 7 methods. For this scenario, you can (or should) use an implementation of the class 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 convenience for Creating Listener Objects.

Which in free translation means:

An abstract adaptor class for receiving window events (window Events). The methods in this class are empty (no implementation). This class exists to create listeners (listeners) of objects in an easy way.

In this situation, you would overwrite only the method windowClosing class Windowadapter.

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

I hope I’ve helped

  • Who implements whom? It was not very clear, and as the question was more specific of the swing Adapters. if possible, I would like the answer to be more focused on them.

  • Hi @Diegof, added an example. I hope it helps.

  • The answer is already complete, but it would be interesting for you to add the translation of the citation in English, for users who do not know the English language, to have a good understanding.

  • I added the translation. I think it will help. Greetings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.