0% found this document useful (0 votes)
14 views

1.2 Event Handling Introduction

Uploaded by

pdisrocking
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

1.2 Event Handling Introduction

Uploaded by

pdisrocking
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Event Handling

What is an Event?
https://www.tutorialspoint.com/swing/swing_event_handling.htm
• Change in the state of an object is known as Event, i.e., event
describes the change in the state of the source.

• Events are generated as a 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 the list, and
– scrolling the page are the activities that causes an event to occur.
Types of Event
• The events can be broadly classified into two categories −

• Foreground Events −
– These events require direct interaction of the user.
– They are generated as consequences of a person interacting with the
graphical components in the 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 −
– These events require the interaction of the end user.
– Operating system interrupts, hardware or software failure, timer
expiration, and operation completion are some examples 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 a code which is known as an 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.
• Source −
– The source is an object on which the event occurs.
– Source is responsible for providing information of the occurred
event to it's handler.
– Java provide us with classes for the source object.

• Listener −
– It is also known as event handler.
– The listener is responsible for generating a response to an event.
– From the point of view of Java implementation, the listener is also
an object. The listener waits till it receives an event.
– Once the 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 a separate piece of code.

• In this model, the 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 who want to receive them.
Steps Involved in Event Handling
• Step 1 − The user clicks the button and the event is
generated.

• Step 2 − The object of concerned event class is created


automatically and information about the source and the
event get populated within the same object.

• Step 3 − Event object is forwarded to the method of the


registered listener class.

• Step 4 − The method is gets executed and returns.


Points to Remember About the Listener
• In order to design a listener class, you 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 any of the predefined interfaces,


then your class cannot act as a listener class for a source
object.
Callback Methods
• These are the methods that are provided by API provider
and are defined by the application programmer and
invoked by the application developer.

• Here the callback methods represent an event method.

• In response to an event, java jre will fire callback method.

• All such callback methods are provided in listener


interfaces.

• If a component wants some listener to listen to its events, the


source must register itself to the listener.
Java Event classes and Listener interfaces
https://www.javatpoint.com/event-handling-in-java

Event Classes Listener Interfaces


ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener

MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Steps to perform Event Handling
– Following steps are required to perform event handling:
• 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){}
• MenuItem
– public void addActionListener(ActionListener a){}
• TextField
– public void addActionListener(ActionListener a){}
– public void s
• 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:
• Within class
• Other class
• Anonymous class
Java event handling by implementing
ActionListener
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){

//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);

//register listener
b.addActionListener(this);//passing current instance

//add components and set size, layout and visibility


add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}
}
• public void setBounds(int xaxis, int yaxis, int width, int
height); have been used in the above example that sets the
position of the component it may be button, textfield etc.
2) Java event handling by outer class
import java.awt.*;
import java.awt.event.*;
class AEvent2 extends Frame{ import java.awt.event.*;
TextField tf;
AEvent2(){
class Outer implements Action
//create components
tf=new TextField();
Listener{
tf.setBounds(60,50,170,20); AEvent2 obj;
Button b=new Button("click me");
b.setBounds(100,120,80,30); Outer(AEvent2 obj){
//register listener
Outer o=new Outer(this); this.obj=obj;
b.addActionListener(o);//passing outer class instance
//add components and set size, layout and visibility }
add(b);add(tf);
setSize(300,300);
public void actionPerformed(Ac
setLayout(null);
setVisible(true);
tionEvent e){
} obj.tf.setText("welcome");
public static void main(String args[]){
new AEvent2(); }
}
} }
3) Java event handling by anonymous class
import java.awt.*;
import java.awt.event.*;
class AEvent3 extends Frame{
TextField tf;
AEvent3(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(50,120,80,30);

b.addActionListener(new ActionListener(){
public void actionPerformed(){
tf.setText("hello");
}
});
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent3();
}
}

You might also like