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

Adv Java Lec-7 Event Handling.c8e5e28

Uploaded by

yashbirari29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Adv Java Lec-7 Event Handling.c8e5e28

Uploaded by

yashbirari29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Adv. Java Lecture-7


Topic: Event Handling
Event Handling:-
 Any O. S. environment that support GUI constantly monitors events.
Such as keystrokes or mouse clicks.
 The operating environment reports these events to the program that
are running.
 Each program then decides what if anything to do in response to these
events.
 Event sources have methods that allow you to register event listener
with them.
 When an event happens to the source, the source sends a notification of
that event to all the listener object that were registered for that event.
 Different event sources can produce different kinds of events. e.g. a
button can send ActionEvent objects, whereas a window can send
WindowEvent objects .

 Here an overview of how event handling in the AWT works.


1. A listener object is an instance of a class that implements a special
interface called a listener interface.
2. An event source is an object that can register listener object and
send them event objects.
3. The event source sends out event objects to all registered listeners
when that event occurs.
4. The listener object will then use the information in the event object
to determine their reaction to the events.
5. You register the listener object with the source object by using
following lines of code.
eventSourceObject.addEventListener(eventListenerObject);

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

ActionEvent:-
 The ActionListener interface contains the one method that is called
when an ActionEvent occurs.
 For an object to listen for action events, it is necessary to call the
addActionListener() method with the class that implements the
ActionListener interface as the parameter.
 The method addActionListener() is implemented by Button, List,
MenuItem, and TextField components.

public abstract void actionPerformed(ActionEvent e)

 The actionPerformed() method is called when a component is selected


or activated.

e.g. ActionListener listener = ……;


Button button = new Button(“OK”);
button.addActionListener(listener);

 To implement the ActionListener interface , the listener class must have a


method called actionPerformed that receives an ActionEvent object as a
parameter.
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
//reaction to button click goes here
}
}

 Whenever the user clicks the button, the Button object creates an
ActionEvent object and calls listener.actionPerformed(event) passing
that event object.
 The button calls the actionPerformed method whenever the user
clicks the button.

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Program to handle ActionEvent using inner class:


import java.awt.*; //Button, Frame
import java.awt.event.*; //ActionListener, ActionEvent

class ButtonTest1 extends Frame


{
Button b = new Button("Ok");
ButtonTest1()
{
setSize(600,500);
setTitle("Info Planet - Button Test");
setLayout(new FlowLayout());
add(b);

ButtonHandler handler = new ButtonHandler(); // create listener obj


b.addActionListener(handler); // register event with component
} // end of constructor

class ButtonHandler implements ActionListener //inner class


{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello World");
}
}

public static void main(String args[])


{
ButtonTest1 f = new ButtonTest1();
f.setVisible(true);
}
}
Output:

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Program to handle ActionEvent using this reference:


import java.awt.*;
import java.awt.event.*;

class ButtonThisTest extends Frame implements ActionListener


{
Button b = new Button("Ok");

ButtonThisTest()
{
setSize(600,500);
setTitle("Info Planet - Button Test");
setLayout(new FlowLayout());
add(b);

b.addActionListener( this);
}

public void actionPerformed(ActionEvent e)


{
System.out.println("Hello ");
}

public static void main(String [] args)


{
ButtonThisTest f = new ButtonThisTest();
f.setVisible(true);
}
}

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Program to handle ActionEvent using Anonymous class:


import java.awt.*;
import java.awt.event.*;

class ButtonAnonymousTest extends Frame


{
Button b = new Button("Ok");

ButtonAnonymousTest()
{
setSize(600,500);
setTitle("Info Planet - Button Test");
setLayout(new FlowLayout());
add(b);

b.addActionListener( new ActionListener() //Anonymous class


{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello World");
}
});
}

public static void main(String [] args)


{
ButtonAnonymousTest f = new ButtonAnonymousTest();
f.setVisible(true);
}
}

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like