0% found this document useful (0 votes)
70 views4 pages

Steps Involved in Event Handling: Java Event Classes and Listener Interfaces

The document discusses event handling in Java. It defines a source as an object that generates events and a listener as an object that responds to events. It lists common Java event classes like ActionEvent and their corresponding listener interfaces like ActionListener. It explains that listeners must register with sources to receive notifications of events. When an event occurs, an event object is passed from the source to the registered listener's callback method.

Uploaded by

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

Steps Involved in Event Handling: Java Event Classes and Listener Interfaces

The document discusses event handling in Java. It defines a source as an object that generates events and a listener as an object that responds to events. It lists common Java event classes like ActionEvent and their corresponding listener interfaces like ActionListener. It explains that listeners must register with sources to receive notifications of events. When an event occurs, an event object is passed from the source to the registered listener's callback method.

Uploaded by

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

 Source - The source is an object on which event occurs.

Source is responsible for


providing information of the occurred event to it's handler. Java provide as with classes
for source object.
 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.

Steps involved 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.

Java Event classes and Listener interfaces

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
Register the component with the Listener

Registration Methods

For registering the component with the Listener, many classes


provide the registration methods. For example:

o Button

o public void addActionListener(ActionListener a){}

o MenuItem

o public void addActionListener(ActionListener a){}

o TextField

o public void addActionListener(ActionListener a){}

o public void addTextListener(TextListener a){}

o TextArea

o public void addTextListener(TextListener a){}

o Checkbox

o public void addItemListener(ItemListener a){}

o Choice

o public void addItemListener(ItemListener a){}

o List

o public void addActionListener(ActionListener a){}

o public void addItemListener(ItemListener a){}


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();
}
}

You might also like