In addition to the inner and anonymous classes, another
useful type of class is the adapter class. Here this applies
in particular to classes used to reduce the code for event listeners.
While the listener architecture has greatly improved
the efficiency and capabilities of event handling, there are some
complications and annoyances that come along with it. In particular,
we find that the listener interfaces hold up to 6 methods that must
be implemented.
Remember than an interface
holds only abstract methods and its implementation requires that
ALL of its methods be implemented, i.e. overridden with real methods.
So for those methods that you are not using, you must still implement
the methods with empty code bodies.
We illustrate this with the following example where
we implement an anonymous MouseListener
class but only need to use one of the methods.
See Java console for output as cursor moves over applet
area.
AnonListenerApplet.java
|
import
javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** Demonstrate the use of an anonyomous listener object.
**/
public class AnonListenerApplet extends JApplet
{
public void init () {
Container content_pane = getContentPane
();
// Create an instance of a JPanel
sub-class
JPanel panel = new JPanel ();
// Add an anonymous MouseListener
object
// to the panel
panel.addMouseListener (
new MouseListener
() {
public void
mouseEntered (MouseEvent e) {
System.out.println (e.toString () );
}
// Though
we don't use these methods, we
// we
must override them with empty code bodies.
public void
mouseClicked (MouseEvent e) {}
public void
mousePressed (MouseEvent e) {}
public void
mouseReleased (MouseEvent e) {}
public void
mouseExited (MouseEvent e) {}
}
);
// And add the panel to the JApplet
panel.
content_pane.add (panel);
} // init
} // class AnonListenerApplet |
We can avoid implementing all of these unneeded
methods by taking advantage of the adapter classes in the java.awt.event
package. For eight of the listener interfaces there is a corresponding
adapter class for each. These adapters simply implement all of
the the methods for the listener interface method with empty code
bodes. Though the adapters are abstract classes, the methods are
real so you only need to override the method(s) of interest.
Here is the same example like the one above except
that it uses a MouseAdapter:
See Java console for output as cursor moves over applet
area.
AdapterApplet.java
|
import
javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** Demonstrate the use of an MouseAdapter. **/
public class AdapterApplet extends JApplet
{
public void init () {
Container content_pane = getContentPane
();
// Create an instance of JPanel
JPanel panel = new JPanel ();
// Add a anonymous MouseAdapter
object
// to the panel
panel.addMouseListener
(
new MouseAdapter
()
{
public void
mouseEntered (MouseEvent e) {
System.out.println
(e.toString ());
}
}
);
// And a panel to the JApplet panel.
content_pane.add (panel);
} // ctor
} //class AdapterApplet
|
Note: Be careful that you properly
spell the method that you are overriding in the adapter. Otherwise,
you are just adding another method to the subclass. It can be hard
to track down this bug in your program.
We don't have space to discuss them further, but here is the list
of listener adapters with links to the Java 5.0 specifications pages:
Latest update: Nov. 3, 2004
|