Difference between MouseListener and MouseAdapter in Java

11,862

Solution 1

I can't seem to understand the difference between the 2 interfaces. Why can't MouseAdapter be implemented like MouseListener and can only be extended

MouseAdapter implements MouseListener.

MouseAdapter:

An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects. Extend this class to create a MouseEvent (including drag and motion events) or/and MouseWheelEvent listener and override the methods for the events of interest

In absence of MouseAdapter, if you implement MouseListener, you have to provide implementation to all of these interface methods.

mouseClicked(MouseEvent e)
mouseDragged(MouseEvent e)
mouseEntered(MouseEvent e)
mouseExited(MouseEvent e)
mouseMoved(MouseEvent e)
mousePressed(MouseEvent e)
mouseReleased(MouseEvent e)
mouseWheelMoved(MouseWheelEvent e)

when would it be wise to use the one and when the other ?

If you want to implement above 8 methods, implement MouseListener. If you want to provide implementation for only some of these 8 methods, use MouseAdapter and override only those methods of interest for you.

e.g. If you are interested only in implementing one event ( or few events) like mouseClicked(MouseEvent e) event, best to use MouseAdapter. If you implement MouseListener interface in this case, you have to provide blank implementation for other methods, which you are not going to implement.

Solution 2

MouseListener is preferred only when you override all abstract methods else MouseAdapter is the preferred choice.

Solution 3

MouseListener is an Interface and MouseAdapter is an implementation of that. You can use the MouseAdapter in every place that you use a MouseListener.

But implementations have details that have be take in count.

Read the javadocs before decide.

MouseListener MouseAdapter

Solution 4

MouseAdapter implements MouseListener already. The advantage to using MouseAdapter is so you don't have to define everything MouseListener forces you to. It's just an ease of use thing.

If you don't need to define every method from MouseListener then it's perfectly fine to use MouseAdapter.

MouseAdapter just contains the empty definitions for you to Override.

Share:
11,862
Soutzikevich
Author by

Soutzikevich

How free are we really? Democracy then degenerates into tyranny where no one has discipline and society exists in chaos. Democracy is taken over by the longing for freedom. Power must be seized to maintain order. A champion will come along and experience power, which will cause him to become a tyrant. The people will start to hate him and eventually try to remove him but will realize they are not able. -Plato Plato talked about Tyranny almost three thousand years ago and yet, we still experience this phenomena in the world of today. It saddens me deeply that these timeless words also apply to the online community I grew to respect; the stackexchange network.

Updated on June 05, 2022

Comments

  • Soutzikevich
    Soutzikevich about 2 years

    I can't seem to understand the difference between the 2 interfaces. Why can't MouseAdapter be implemented like MouseListener and can only be extended? I'm fairly new to Java.

    Considering we want to perform an action that can be completed with either of these 2 interfaces.

    Also, when would it be wise to use the one and when the other ?

  • Soutzikevich
    Soutzikevich about 7 years
    So that would mean that Mouse Adapter is not an interface, but a method?
  • Soutzikevich
    Soutzikevich about 7 years
    Thank you. I seem to understand how this interface works now :) Thank you for the thorough explanation !