AJP Chapter 3
AJP Chapter 3
Event Handling
Made By
Date:- 24/08/2020
What is an Event?
Event Source
Listener 3 Take action 3
event model • In the delegation event model, listeners must register with a source in order to
receive an event notification.
• This provides an important benefit: notifications are sent only to listeners that
want to receive them.
• This is a more efficient way to handle events than the design used by the
original Java 1.0 approach.
Previously, an event was propagated up the containment hierarchy until it was
handled by a component.
• This required components to receive events that they did not process, and it
wasted valuable time.
• The delegation event model eliminates this overhead.
• The User clicks the button and the event is
generated.
• Now the object of concerned event class is
Steps created automatically and information about
involved in the source and the event get populated with
in same object.
event • Event object is forwarded to the method of
handling registered listener class.
• the method is now get executed and returns.
Event Listener
• The events generated by the GUI components are handled by a
special group of interfaces known as “listeners”.
• A listener is an object that is notified when an event occurs.
• 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.
2.
The MouseListener interface is found in
MouseListener java.awt.event package.
Interface
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
WindowAdapter Example1
import java.awt.*;
import java.awt.event.*;
public class AdapterExample{ OUTPUT
Frame f;
AdapterExample(){
f=new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
f.dispose();
} });
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new AdapterExample();
} }
MouseAdapter Example2
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter{
Frame f; Output:
MouseAdapterExample(){
f=new Frame("Mouse Adapter");
f.addMouseListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true); }
public void mouseClicked(MouseEvent e) {
Graphics g=f.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
public static void main(String[] args) {
new MouseAdapterExample();
} }
• We can close the AWT Window or Frame by
calling dispose() or System.exit() inside windowClosing()
method. The windowClosing() method is found
in WindowListener interface and WindowAdapter class.
• The WindowAdapter class implements WindowListener
interfaces. It provides the default implementation of all the 7
How to close methods of WindowListener interface. To override the
windowClosing() method, you can either use WindowAdapter
class or WindowListener interface.
AWT • If you implement the WindowListener interface, you will be
Window forced to override all the 7 methods of WindowListener
interface. So it is better to use WindowAdapter class.
There are many ways to override windowClosing() method:
• By anonymous class
• By inheriting WindowAdapter class
• By implementing WindowListener interface
Close AWT Window Example 1: Anonymous class
import java.awt.*;
import java.awt.event.*;
public class WindowExample extends Frame{
WindowExample(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
}
});
setSize(400,400);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new WindowExample();
}
Close AWT Window Example 2: extending WindowAdapter
import java.awt.*;
import java.awt.event.*;
public class AdapterExample extends WindowAdapter{
Frame f;
AdapterExample(){
f=new Frame();
f.addWindowListener(this);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void windowClosing(WindowEvent e) {
f.dispose();
}
public static void main(String[] args) {
new AdapterExample();
} }
Close AWT Window Example 3: implementing
WindowListener
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowExample extends Frame implements WindowListener{
WindowExample(){
addWindowListener(this); OUTPUT
setSize(400,400);
setLayout(null);
setVisible(true); }
public static void main(String[] args) {
new WindowExample(); }
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
dispose(); }
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent arg0) {} }
Text Listener Interface