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.