Listening for a click without writing six empty methods
A mouse listener interface has several methods, but most programs only care about one — a click. Adapter classes let you override only the method you need, ignoring the rest.
After this lesson
You should be able to
- Explain why MouseListener has several methods instead of one.
- Use a MouseAdapter to handle only a click, without implementing every method.
- Explain the trade-off adapter classes make.
An interface with more methods than you usually need
MouseListener declares five methods: mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited. Implementing an interface, from Unit II, means fulfilling every method it declares — so a class implementing MouseListener directly must write all five, even if it only cares about clicks, leaving four empty method bodies as pure ceremony.
This is the real cost of a wide interface, and it is exactly the kind of boilerplate Java tries to reduce with a second option: an adapter class.
MouseAdapter: an empty implementation you extend, not implement
MouseAdapter is a class, not an interface, that already implements MouseListener with an empty body for every method. Extending MouseAdapter and overriding only mouseClicked gives you exactly the one method you wanted, with the other four already silently satisfied by the empty versions you inherited — the same override mechanism from Unit II, applied to skip boilerplate rather than to add new behaviour.
The trade-off: a class can only extend one thing, so if MouseAdapter has already used your one extends, you cannot also extend some other class. Reach for the interface directly, empty methods and all, when you genuinely need to extend something else too.
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked at " + e.getX() + ", " + e.getY());
}
// no need to write mousePressed, mouseReleased, mouseEntered, mouseExited
});Try it yourself
Using MouseAdapter, print "Mouse entered the panel" only when the mouse enters a panel's boundary, without implementing any of the other four MouseListener methods.
Need a hint?
Override only mouseEntered — everything else, including mouseClicked, stays as the empty inherited version and simply does nothing.
Check the worked solution
Extending MouseAdapter and overriding only mouseEntered means the other four methods are still technically present, inherited from MouseAdapter, doing nothing — exactly the payoff of using an adapter over a raw interface implementation.
panel.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered the panel");
}
});Quick check
Why does extending MouseAdapter and overriding only mouseClicked not cause a compiler error, when implementing MouseListener directly would require all five methods?
Why this lesson exists
Syllabus mapping
handling mouse and keyboard events, Adapter classes.
Maps to course outcome CO4.