Event adapters facilitate implementing listener interfaces.
Many event listener interfaces have more than one event listener methods. For
such interfaces, Java technology defines adapter classes. These have empty
implementation of all the event listener methods defined in the interface they
implement. A listener can subclass the adapter and override only stub methods
for handling events of interest. The table below lists the low level event
listener interfaces and their adapters.
The
table below shows the adapter classes in java.awt.event
and notes the interface that each implements.
Adapter Class
|
Listener Interface
|
ComponentAdapter
|
ComponentListener
|
ContainerAdapter
|
ContainerListener
|
FocusAdapter
|
FocusListener
|
KeyAdapter
|
KeyListener
|
MouseAdapter
|
MouseListener
|
MouseMotionAdapter
|
MouseMotionListener
|
WindowAdapter
|
WindowListener
|
Example:
import
java.awt.event.*;
import
java.applet.*;
import
java.awt.*;
public
class MyAdapterApplet extends Applet
{
public void init()
{
addMouseListener(new MyAdapter(this));
}
}
class
MyAdapter extends MouseAdapter
{
MyAdapterApplet ap;
public MyAdapter(MyAdapterApplet adp)
{
this.ap=adp;
}
public void mouseClicked(MouseEvent e)
{
ap.showStatus("Mouse Clicked");
}
}
No comments:
Write comments