Java Event Handling
Java Event Handling
What is an Event?
Change in the state of an object is known as event i.e. event
describes the change in state of source. Events are generated as
result of user interaction with the graphical user interface
components. For example, clicking on a button, moving the
mouse, entering a character through keyboard, selecting an item
from list, scrolling the page are the activities that causes an event
to happen.
In other words
Any program that utilizes GUI (graphic user interface) like
windows-written Java application is event oriented. Event defines
any object's change in status. For example: press a button, enter
a character in textbox, click or drag a mouse, etc.
Types of Event
The events can be broadly classified into two categories:
Foreground Events - Those events which require the direct
interaction of user. They are generated as consequences of
a person interacting with the graphical components in
Graphical User Interface. For example, clicking on a button,
moving the mouse, entering a character through keyboard,
selecting an item from list, scrolling the page etc.
Background Events - Those events that require the indirect
interaction of end user are known as background events.
Operating system interrupts, hardware or software failure,
timer expires, an operation completion are the example of
background events.
What is 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.
Java Uses the Delegation Event Model to handle the events.
This model defines the standard mechanism to generate and
handle the events.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <applet code="MListen1.class" width=500 height=500>
</applet> */
// Key Listener
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="KListen.class" width=500 height=500>
</applet> */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;