Difference between MouseListener and MouseAdapter in Java
Asked Answered
T

4

5

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 ?

Tabber answered 30/5, 2017 at 17:41 Comment(2)
Simple google foo would have told you the answer. My google foo shows thisAphid
So, a mouse listener is an interface. That means it has no content. A interface is a basic structure. It does only contain method names. So, java sees, you implement MouseListener, what means, you have to have its methods, like released, so that they can be called by AWT/swingReverse
P
12

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.

Purapurblind answered 30/5, 2017 at 18:19 Comment(1)
Thank you. I seem to understand how this interface works now :) Thank you for the thorough explanation !Tabber
G
4

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

Garris answered 30/5, 2017 at 17:53 Comment(0)
C
2

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

Chromomere answered 30/5, 2017 at 17:49 Comment(1)
So that would mean that Mouse Adapter is not an interface, but a method?Tabber
B
1

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.

Bracket answered 30/5, 2017 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.