UNIT5-Part2-Java
UNIT5-Part2-Java
A mechanism for controlling the events and deciding what should happen after an event occur is referred to as event handling. Java follows the
Delegation Event Model for handling the events.
Events
Events Sources
Events Listeners
Event Sources
A source is an object that causes and generates events and sends to the Listener. The sources are allowed to generate several different types of
events.
A source must register a listener to receive notifications for a specific event. Each event contains its registration method. Below is an example:
From the above syntax, the Type is the name of the event, and e1 is a reference to the event listener. For example, for a keyboard event
listener, the method will be called as addKeyListener().
Event Listeners
An event listener is an object that is invoked when an event triggers. The listeners require two things; first, it must be registered with a source;
however, it can be registered with several resources to receive notification about the events. Second, it must implement the methods to
receive and process the received notifications.
To handle mouse events, we must implement the MouseListener and the MouseMotionListener interfaces and provide the implementations of
the below events.
To handle keyboard events we must implements KeyListener interface When a key is pressed, a KEY_PRESSED event is generated and the
keyPressed( ) handler is executed. When the key is released, a KEY_RELEASED event is generated and the keyReleased( ) handler is executed. If
a character is generated by the keystroke, then a KEY_TYPED event is sent and the keyTyped( ) handler is invoked.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Adapter Classes
Java provides a special feature, called an adapter class, that can simplify the creation of event handlers in certain situations.
An adapter class provides an empty implementation of all methods in an event listener interface.
Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener
interface.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="AdapterDemo" width=300 height=100>
</applet>*/
Inner Classes
import java.applet.*;
import java.awt.event.*;
/*
<applet code="InnerClassDemo" width=200 height=100>
</applet>
*/
public class InnerClassDemo extends Applet {
public void init() {
addKeyListener(new MyKeyAdapter());
}
class MyKeyAdapter extends MouseAdapter {
public void keyReleased(KeyEvent me) {
showStatus("Key is Released");
}
}
}
An anonymous inner class is one that is not assigned a name. below program describes how an anonymous inner class can facilitate the writing
of event handlers.
import java.applet.*;
import java.awt.event.*;
/*<applet code="AnonymousInnerClassDemo" width=200 height=100>
</applet>
*/
public class AnonymousInnerClassDemo extends Applet {
public void init() {
addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent me) {
showStatus("Key is Released");
}
});