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.
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 do not 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.
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.
Event handling
in Java programming because is integral to the creation of applets and other
sorts of GUI-based programs.
The java.awt.event package provides many event
classes and Listener interfaces for event handling.
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 event occurs. Source is
responsible for providing information of the occurred event to its
handler.
- Listener - It is also known as event handler. Listener is responsible
for generating response to an event. 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.
For registering the component with the Listener,
many classes provide the registration methods, such as:
- Button
public void addActionListener(ActionListener a){}
- MenuItem
public void addActionListener(ActionListener a){}
- TextField
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){}
The event handling code can be placed in one of the
following places:
- Within class
- Other class
- Anonymous class
Ex1: Within class
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();
}
}
Ex2: Java
event handling by outer class
import java.awt.*;
import java.awt.event.*;
class AEvent2 extends Frame{
TextField tf;
AEvent2(){
//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
Outer o=new Outer(this);
b.addActionListener(o);//passing outer class
instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent2();
}
}
Outer
class to handle event:
import java.awt.event.*;
class Outer implements ActionListener{
AEvent2 obj;
Outer(AEvent2 obj){
this.obj=obj;
}
public void actionPerformed(ActionEvent e){
obj.tf.setText("welcome");
}
}
No comments:
Post a Comment