0% found this document useful (0 votes)
31 views18 pages

VIPS OOPS Unit 3 Event Handling and Listeners

The document provides an overview of event handling in Java, detailing the roles of event sources, listeners, and the ActionEvent class. It explains how to register listeners for specific events, the structure of event classes, and demonstrates creating a frame window within an applet. Additionally, it introduces adapter classes and anonymous inner classes for simplifying event handling by allowing selective method implementation.

Uploaded by

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

VIPS OOPS Unit 3 Event Handling and Listeners

The document provides an overview of event handling in Java, detailing the roles of event sources, listeners, and the ActionEvent class. It explains how to register listeners for specific events, the structure of event classes, and demonstrates creating a frame window within an applet. Additionally, it introduces adapter classes and anonymous inner classes for simplifying event handling by allowing selective method implementation.

Uploaded by

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

Event Sources

public void addTypeListener(TypeListener el)


Type is the name of the event, and
 el is a reference to the event listener.
When such an event occurs, the registered listener is notified.
This is known as unicasting the event.
A source must also provide a method that allows a listener to
unregister an interest in a specific type of event.
The general form of such a method is this:
public void removeTypeListener(TypeListener el)
Events
What is an Event?
An event is an object that describes a state change in a source.

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.
Event Sources
A source is an object that generates an event. This occurs when the internal
state of that object changes in some way. Sources may generate more than one
type of event.
public void addTypeListener(TypeListener el)
Event Listeners

• A listener is an object that is notified when an event occurs. It has two


major requirements.
• First, it must have been registered with one or more sources to receive
notifications about specific types of event.
• Second, it must implement methods to receive and process these
notifications.
• For example, the MouseMotionListener interface defines two
methods to receive notifications when the mouse is dragged or
moved.
The ActionEvent Class

• An ActionEvent is generated when a button is pressed, a list item is


double-clicked, or a menu item is selected. The ActionEvent class
defines four integer constants that can be used to identify any
modifiers associated with an action event: ALT_MASK,
CTRL_MASK, META_MASK, and SHIFT_MASK.
Event Classes
At the root of the Java event class hierarchy is EventObject, which is in
java.util. It is the superclass for all events. Its one constructor is shown
here:
EventObject(Object src)
Here, src is the object that generates this event.
EventObject contains two methods: getSource( ) and toString( ). The
getSource( ) method returns the source of the event. Its general form is
shown here:
Object getSource( )
 toString( ) returns the string equivalent of the event.
Table:Main Event Classes in
java.awt.event
The ActionEvent Class

• ActionEvent has these three constructors:


ActionEvent(Object src, int type, String cmd)
ActionEvent(Object src, int type, String cmd, int modifiers)
ActionEvent(Object src, int type, String cmd, long when, int modifiers)
• src is a reference to the object that generated this event.
• The type of the event is specified by type, and its command string is cmd.
• The argument modifiers indicates which modifier keys (ALT, CTRL,
META, and/or SHIFT) were pressed when the event was generated.
• The when parameter specifies when the event occurred.
Button event
• A push button is a component that contains a label and that generates
an event when it is pressed. Push buttons are objects of type Button.
Button defines these two constructors:
• Button( ) throws HeadlessException
• Button(String str) throws HeadlessException
• The first version creates an empty button.
• The second creates a button that contains str as a label.
Example of Creating a Frame
Window in an Applet

Create a subclass of Frame.


Override any of the standard applet methods, such as init( ),
start( ), and stop( ).
Implement the windowClosing( ) method of the WindowListener
interface, calling setVisible(false) when the window is closed.
Example of Creating a Frame Window in an Applet
• // Create a child frame window from within an applet. class MyWindowAdapter extends WindowAdapter {
import java.awt.*; SampleFrame sampleFrame;
import java.awt.event.*; import java.applet.*; /* public MyWindowAdapter(SampleFrame
sampleFrame)
<applet code="AppletFrame" width=300 height=50>
{ this.sampleFrame = sampleFrame;}
</applet> */
public void windowClosing(WindowEvent we)
// Create a subclass of Frame.
{ sampleFrame.setVisible(false); } }
class SampleFrame extends Frame { // Create frame window.
SampleFrame(String title) { super(title); public class AppletFrame extends Applet {
// create an object to handle window events Frame f;
MyWindowAdapter adapter = new MyWindowAdapter(this); public void init() {
// register it to receive those events
f = new SampleFrame("A Frame Window");
f.setSize(250, 250); f.setVisible(true);}
addWindowListener(adapter);
public void start() { f.setVisible(true); }
}
public void stop() { f.setVisible(false); }
public void paint(Graphics g) { public void paint(Graphics g) {
g.drawString("This is in frame window", 10, 40); g.drawString("This is in applet window", 10, 20); } }
}}
Creating a Frame Window in an Applet

Output :

The following applet creates a subclass


of Frame called SampleFrame.
A window of this subclass is instantiated
within the init( ) method of AppletFrame.
Notice that SampleFrame calls Frame’s
constructor. This causes a standard frame
window to be created with the title
passed in title. This example overrides
the applet’s start( ) and stop( ) methods
so that they show and hide the child
window, respectively.
Adapter Anonymous Class
 One most important special feature provided by Java, called an adapter class.
Adapter classes are useful when you want to receive and process only some of the
events not all that are handled by a particular event listener interface (if you have 5
event method for particular event e.g MouseEvent but you want to implement
only 1 or 2 method only) .
For example, the MouseMotionAdapter class has two methods,
 mouseDragged( )
 mouseMoved( )
 These are the methods defined by the MouseMotionListener interface. If you were
interested in only mouse drag events, then you could simply extend
MouseMotionAdapter and override mouseDragged( ). The empty implementation(not
implemented) of mouseMoved( ) would handle the mouse motion events for you.
Adapter Anonymous Class

• Its init( ) method creates an instance of MyMouseAdapter and


registers that object to receive notifications of mouse events. It also
creates an instance of MyMouseMotionAdapter and registers that
object to receive notifications of mouse motion events.
Annonymous adapter Class Example
// Demonstrate an adapter. // Handle mouse clicked.
import java.awt.*; public void mouseClicked(MouseEvent me) {
import java.awt.event.*; adapterDemo.showStatus("Mouse clicked");
import java.applet.*;
/*applet code="AdapterDemo" width=300 height=100>
}}
</applet>*/ class MyMouseMotionAdapter extends
public class AdapterDemo extends Applet { MouseMotionAdapter {
public void init() { AdapterDemo adapterDemo;
addMouseListener(new MyMouseAdapter(this)); public MyMouseMotionAdapter(AdapterDemo
addMouseMotionListener(new adapterDemo) {
MyMouseMotionAdapter(this));
}}
this.adapterDemo = adapterDemo;
class MyMouseAdapter extends MouseAdapter { }
AdapterDemo adapterDemo; // Handle mouse dragged.
public MyMouseAdapter(AdapterDemo adapterDemo) { public void mouseDragged(MouseEvent me) {
this.adapterDemo = adapterDemo;
}
adapterDemo.showStatus("Mouse dragged");
}}
Annonymous adapter Class Example
• Note :In the given example
Output: MouseMotionListener
and MouseListener interfaces
saves you a considerable amount
of effort and prevents your code
from becoming cluttered with
empty methods.
Anonymous Inner Classes Example

An anonymous inner class is // Anonymous inner class demo.


one that is not assigned a name. import java.applet.*;
import java.awt.event.*;
 Consider the applet shown in
/*<applet code="AnonymousInnerClassDemo"
the following Example listing. width=200 height=100></applet>*/
Given example goal is to display public class AnonymousInnerClassDemo extends
the string “Mouse Pressed” in Applet { public void init() {
the status bar of the applet addMouseListener(new MouseAdapter() {
viewer or browser when the public void mousePressed(MouseEvent me) {
mouse is pressed. showStatus("Mouse Pressed");}
});}}

You might also like