Programming in Java
CBCS BHCS03
By Parul Chachra
Chapter 24
Event Handling
Reference: Book prescribed as per
the syllabus
Adapter Classes
• Commonly used Adapter classes are:
Adapter Classes
• The following program demonstrates an adapter
• It displays a message in the status bar of an applet viewer or browser when the mouse is clicked or dragged
• However, all other mouse events are silently
ignored
Adapter Classes
• The following program demonstrates an adapter
• The program has three classes
• AdapterDemo extends Applet
• Its init( ) method creates an instance of
MyMouseAdapter and registers that object to receive noKficaKons of mouse events
• It also creates an instance of
MyMouseMo5onAdapter and registers that object to receive noKficaKons of mouse
moKon events
Adapter Classes
• MyMouseAdapter extends MouseAdapter and overrides the mouseClicked( ) method
• The other mouse events are silently ignored by code inherited from the MouseAdapter class
• MyMouseMo5onAdapter extends
MouseMo5onAdapter and overrides the mouseDragged( ) method
• The other mouse moKon event is silently ignored by code inherited from the
MouseMo5onAdapter class
• Note that both of the event listener classes save a reference to the applet
• This informaKon is provided as an argument to
their constructors and is used later to invoke the
showStatus( ) method
Adapter Classes
// Demonstrate an adapter.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AdapterDemo" width=300 height=100>
</applet>
*/
public class AdapterDemo extends Applet { public void init() {
Init() is used to iniKalise the applet. It’s a predefined funcKon of Applet class
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
}
}
Adapter Classes
// Demonstrate an adapter.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AdapterDemo" width=300 height=100>
</applet>
*/
public class AdapterDemo extends Applet { public void init() {
addMouseListener(new MyMouseAdapter(this));
The above statement is Registering for the listener for the Mouse event.
this is a reference to the applet itself.
addMouseMotionListener(new MyMouseMotionAdapter(this));
The above statement is Registering for the listener for the MouseMoKon event. this is a reference to the applet itself.
} }
Adapter Classes
class MyMouseAdapter extends MouseAdapter { AdapterDemo adapterDemo;
Object of the applet which has registered for the mouse and mousemoKon listeners.
This is the source of the event
public MyMouseAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo;
}
addMouseListener() calls the constructor of MyMouseAdapter class enabling an associaKon between the acKve applet and the listener interface via an object of this class
public void mouseClicked(MouseEvent me) {
adapterDemo.showStatus("Mouse clicked");
}
• The adapter class enables to override only some of the funcKons defined by the mouse listener interface. Hence, the adapter class only overrides mouseClicked().
• AdapterDemo points to the acKve applet and the showStatus() is used to print
“MOUSE CLICKED” on the status bar of the applet
}
Adapter Classes
class MyMouseMotionAdapter extends MouseMotionAdapter { AdapterDemo adapterDemo;
Object of the applet which has registered for the mousemoKon listeners. This is the source of the event
public MyMouseMotionAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo;
}
addMouseMoKonListener() calls the constructor of MyMouseMoKonAdapter class enabling an associaKon between the acKve applet and the listener interface via an object of this class
// Handle mouse dragged.
public void mouseDragged(MouseEvent me) { adapterDemo.showStatus("Mouse dragged");
}
• The adapter class enables to override only some of the funcKons defined by the
mousemoKon listener interface. Hence, the adapter class only overrides mouseDragged().
• AdapterDemo points to the acKve applet and the showStatus() is used to print “MOUSE DRAGGED” on the status bar of the applet
}
}