Java Group7-1
Java Group7-1
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
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.
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.
For registering the component with the Listener , many classes provide the registration methods.
For example:
Button
Menu Item
Text Field
TextArea
Checkbox
Choice
List
We can put the event handling code into one of the following places:
1. Within class
2. Other class
3. Anonymous class
Example
import java.awt.*;
import java.awt.event.*;
TextField tftough;
AEvent(){
//create components
tftough.setBounds(60,50,170,20);
butsetBounds(100,120,60,20);
//register listener
but.addActionListener(this);
add(but);add(tftough);
setSize(300,300);
setLayout(null);
setVisible(true); }
public void actionPerformed(ActionEvent e){
new AEvent(); } }
import java.awt.event.*;
TextField tftough;
Eve2(){
//create components
tftough.setBounds(60,50,170,20);
b.setBounds(100,120,60,20);
//register listener
Outer oouter=new Outer(this);
b.addActionListener(oouter);//passing outer class instance
add(b);
add(tftough);
import java.awt.event.*;
Eve2 object;
Outer(Eve2 object){
this.object=object; }
}}
import java.awt.event.*;
TextField tfen;
Event3(){
tfen=new TextField();
tfen.setBounds(60,50,170,20);
b.setBounds(50,120,80,30);
b.addActionListener(new ActionListener(){
tfen.setText("sanitize");
} });
add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); }
Interfaces
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.