1.2 Event Handling Introduction
1.2 Event Handling Introduction
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.
• 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 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.
• In this model, the listener needs to be registered with the source object
so that the listener can receive the event notification.
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
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();
}
}