What is event handling? Explain two ways of handling events briefly with suitable example,
EVENT HANDLING
Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism has the code which is known as event handler that is executed when an event occurs. In java, events represent all the interactions between an application and its user. When a user interacts with a program the system creates an event that represents the action and delegates it to the event handling code within the program.
Two EVENT HANDLING MECHANISMS
The way of handling events changed significantly between the original version (i.e. Java 1.0) of java and the modern version of java. This new way of handling events is introduced from Java 1.1. The methods that are used in 1.0 event handling model is still supported but not recommended in modern Java program. These methods have been deprecated in modern versions of Java. This modern approach of handling event is called event delegation model. In brief, two event handling mechanism used in java are: a)Old way of event Handling and b) Event Delegation Model.
a) Event Delegation Model
Modern versions of Java use the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events. In this model à source generates an event and sends it to one or more listeners. The listener simply waits until it receives an event. Once an event is received, the listener processes the event and then returns. The benefit of this approach is that the user interface logic is completely separated from the logic that generates the event. The user interface element is able to delegate the processing of an event to the separate piece of code. In Delegation model Listener needs to be registered with the source object so that the listener can receive the event notification. This is an efficient way of handling the event because the event notifications are sent only to those listeners that want to receive them.
b)Old way of event Handling
This approach uses a non-delegated approach for handling events. In this model, we do not need to register to get events. When an event has occurred we need to handle them in a method named action. The major problem of this approach is that the action method becomes huge because all events need to be handled by this method. The example given below shows this approach to handling events. The rest of the examples presented in this book will use event delegation model.
Example
import java.awt.*;
public class Event Test extends Frame
{
TextField t;
Button b;
Event Test()
{
setTitle("Handle Events Old Way");
setSize(300,200);
set Layout (new FlowLayout (FlowLayout.CENTER, 25, 50));
add (b);
t=new TextField (20);
b=new Button("Click");
add (t);
setVisible (true);
}
public boolean action (Event e, Object obj)
{
String caption=(String) obj; String msg="Hello";
if (e.target instanceof Button)
{ //checking whether event target is button or not if (caption=="Click")
{
t.setText (msg);
}
}
return true;
}
public static void main(String args[]) {
new Event Test();
}
}
Comments
Post a Comment