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

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Suppose that a data warehouse consists of the three dimensions time, doctor, and patient, and the two measures count and charge, where a charge is the fee that a doctor charges a patient for a visit. a) Draw a schema diagram for the above data warehouse using one of the schemas. [star, snowflake, fact constellation] b) Starting with the base cuboid [day, doctor, patient], what specific OLAP operations should be performed in order to list the total fee collected by each doctor in 2004? c) To obtain the same list, write an SQL query assuming the data are stored in a relational database with the schema fee (day, month, year, doctor, hospital, patient, count, charge)

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?