Can you use an Adapter inside of a Listener class?

353 views Asked by At

I was wondering if you can use an adapter, say a MouseAdapter inside of a class that implements MouseListener.

I know I can use the Adapter as an anonymous Listener

    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            panel.setBackground(Color.BLACK);
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            panel.setBackground(Color.WHITE);
            repaint();
        }
    });

But I was wondering if I can define a separate Listener class without having to override all the other abstract methods, like Below

    private class myListener implements MouseListener {
        @Override
        public void mousePressed(MouseEvent e) {
            panel.setBackground(Color.BLACK);
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            panel.setBackground(Color.WHITE);
            repaint();
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
}
1

There are 1 answers

2
dantuch On BEST ANSWER

Sure you can, because:

public abstract class MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener

It does implement MouseListener.

from http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/awt/event/MouseAdapter.java :

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

Mouse events let you track when a mouse is pressed, released, clicked, moved, dragged, when it enters a component, when it exits and when a mouse wheel is moved.

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. (If you implement the MouseListener, MouseMotionListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.)

Create a listener object using the extended class and then register it with a component using the component's addMouseListener addMouseMotionListener, addMouseWheelListener methods. The relevant method in the listener object is invoked and the MouseEvent or MouseWheelEvent is passed to it in following cases:

  • when a mouse button is pressed, released, or clicked (pressed and released)
  • when the mouse cursor enters or exits the component
  • when the mouse wheel rotated, or mouse moved or dragged

EDIT:

If your application only needs to know whether the mouse is pressed or released over a component, the other three methods will be empty and ignored. Those methods are unnecessary code. The adapter classes can help reduce the amount of code you must write when your application needs only a small subset of all interface methods. Each adapter class fully implements its associated interface (or interfaces). Then, if you want a listener for a subset of associated methods, you just have to provide that subset. No empty stubs required. Here is just such an adapter for the required MouseListener previously described.

MouseListener mouseListener = new MouseAdapter() {
  public void mousePressed(MouseEvent mouseEvent) {
      System.out.println("I'm pressed: " + mouseEvent);
  }
  public void mouseReleased(MouseEvent mouseEvent)  {
      System.out.println("I'm released: " + mouseEvent);
  }
};

https://blogs.oracle.com/CoreJavaTechTips/entry/listeners_vs_adapters