Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
We use a nested class, MA,...
that extends java.awt.event.MouseAdapter .
The advantage is, that we only have to specify the methods that we're interested in. In this case, mouseClicked.
implements java.awt.event.MouseListener ...
because the MouseAdapter class implements it, it contains the 5 mouse methods. They're empty, though, nothing happens.
An instance of MA is registered as listener ...
instead of addMouseListener ( this ) , for example.
You can NOT capture mouse MOTION with the technique shown here, for that you need the MouseMotionListener of the next lesson.
Inner class methods have access to all members of the containing(outer) class.
After compiling there is an extra binary file, Adapt$MA.class .
What we actually do here is OVERRIDE MousAdapter's ( empty ) mouseClicked method.
Here are some of the other adapter classes, in order of usefulness, with associated methods :
This implements the WindowListener interface, containing :
windowClosing ( WindowEvent )
Invoked when a window is about to be closed. Opportunity to save work.
windowIconified ( WindowEvent )
Invoked when a window is minimized.
windowDeiconified ( WindowEvent )
The other way around.
windowActivated ( WindowEvent )
windowDeactivated ( WindowEvent )
windowClosed ( WindowEvent )
When a window has already been closed.
windowOpened ( WindowEvent ).
This implements the KeyListener interface, containing :
keyPressed ( KeyEvent )
keyReleased ( KeyEvent )
keyTyped ( KeyEvent )