An example of Adapter Classes

Here's a mouse adapter that beeps when the mouse is clicked

import java.awt.*;
import java.awt.event.*;


public class MouseBeeper extends MouseAdapter  {

  public void mouseClicked(MouseEvent e) {
    Toolkit.getDefaultToolkit().beep();
  }

}
Without extending the MouseAdapter class, I would have had to write the same class like this

import java.awt.*;
import java.awt.event.*;


public class MouseBeeper implements MouseListener  {

  public void mouseClicked(MouseEvent e) {
    Toolkit.getDefaultToolkit().beep();
  }

  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}

}
Adapter classes are a minor convenience. You do not need to use the adapter classes if you don't want to.


Previous | Next | Top
Last Modified August 7, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@sunsite.unc.edu