Home : Course Map : Chapter 7 : Java :
Adapter Classes
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

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

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.