MIDLANDS STATE UNIVERSITY
FACULTY OF SCIENCE AND TECHNOLOGY
DEPARTMENT OF INFORMATION SYSTEMS AND COMPUTER SCIENCE
NAME SURNAME REG NO MODE OF ENTRY
Tanaka Mapaike R165564M CON
Bright Kabasa R169884T CON
Chiedza V Chiwaridzo R164924R CON
Brenda Chaka R163360F CON
Nyasha Mupezeni R167210v CON
Panashe Mutsvangwa R167292Z CON
Prince Mataruse R166242V CON
Benedict Tapfuma R143890T CON
Lawrence Nyajambwa R163425R CON
Isheanesu Makwara R169276Q CON
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.
Types of Events
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 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 have 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.
The Delegation Event Model has the following key participants namely:
Source - The source is an object on which event occurs. Source is responsible for providing
information of the occurred event to it's handler.
Listener - It is also known as event handler. Listener is responsible for generating response to an
event. From java implementation point of view the listener is also an object. Listener waits until
it receives an event. Once the event is received, the listener process the event an 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 this 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.
Points to remember in event handling
The User clicks the button and the event is generated.
Now the object of concerned event class is created automatically and information about
the source and the event get populated with in same object.
Event object is forwarded to the method of registered listener class. the method is now
get executed and returns.
Points to remember about listener
In order to design a listener class we have to develop some listener interfaces.
These Listener interfaces forecast some public abstract callback methods which must be
implemented by the listener class
If you do not implement the any if the predefined interfaces, then your class can not act
as a listener class for a source object.
Event Handling Steps to follow
Following steps are required to perform event handling:
1. Register the component with the Listener
Registration Methods
For registering the component with the Listener , many classes provide the registration methods.
For example:
Button
public void addActionListener(ActionListener a){}
Menu Item
public void addActionListener(ActionListener a){}
Text Field
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
Java Event Handling Code
We can put the event handling code into one of the following places:
1. Within class
2. Other class
3. Anonymous class
Example
1.Java handling method within class
Java event handling by implementing ActionListener
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tftough;
AEvent(){
//create components
tftough =new TextField();
tftough.setBounds(60,50,170,20);
Button but=new Button("press");
butsetBounds(100,120,60,20);
//register listener
but.addActionListener(this);
//passing current instance
//add components and set size, layout and visibility
add(but);add(tftough);
setSize(300,300);
setLayout(null);
setVisible(true); }
public void actionPerformed(ActionEvent e){
tftough.setText("COVID 19 will go soon");
} public static void main(String args[]){
new AEvent(); } }
2) Java event handling by outer class
import java.awt.*;
import java.awt.event.*;
class Eve2 extends Frame{
TextField tftough;
Eve2(){
//create components
tftough =new TextField();
tftough.setBounds(60,50,170,20);
Button b=new Button("cpress");
b.setBounds(100,120,60,20);
//register listener
Outer oouter=new Outer(this);
b.addActionListener(oouter);//passing outer class instance
//add components and set size, layout and visibility
add(b);
add(tftough);
setSize(300,300); setLayout(null); setVisible(true); }
public static void main(String args[]){ new Eve2(); } }
import java.awt.event.*;
class Outer implements ActionListener{
Eve2 object;
Outer(Eve2 object){
this.object=object; }
public void actionPerformed(ActionEvent e)
{ object.tftough.setText("Keep Social Distance");
}}
3) Java event handling by anonymous class
import java.awt.*;
import java.awt.event.*;
class Event3 extends Frame{
TextField tfen;
Event3(){
tfen=new TextField();
tfen.setBounds(60,50,170,20);
Button b=new Button("press");
b.setBounds(50,120,80,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(){
tfen.setText("sanitize");
} });
add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); }
public static void main(String args[]){ new Event3(); } }
Interfaces
Event and Listener (Java Event Handling)
Changing the state of an object is known as an event. For example, click on button, dragging
mouse etc. The java.awt.event package provides many event classes and Listener interfaces for
event handling.
Important Event Classes and Interface
Event Classes Description Listener Interface
ActionEvent generated when button is pressed, menu-item is ActionListener
selected, list-item is double clicked
MouseEvent generated when mouse is dragged, MouseListener
moved,clicked,pressed or released and also when it
enters or exit a component
KeyEvent generated when input is received from keyboard KeyListener
ItemEvent generated when check-box or list item is clicked ItemListener
TextEvent generated when value of textarea or textfield is TextListener
changed
MouseWheelEve generated when mouse wheel is moved MouseWheelListener
nt
WindowEvent generated when window is activated, deactivated, WindowListener
deiconified, iconified, opened or closed
ComponentEvent generated when component is hidden, moved, resized ComponentEventListener
or set visible
ContainerEvent generated when component is added or removed from ContainerListener
container
AdjustmentEvent generated when scroll bar is manipulated AdjustmentListener
FocusEvent generated when component gains or loses keyboard FocusListener
focus