Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

UNIT5-Part2-Java

The document explains event handling in Java, detailing the Delegation Event Model which consists of events, event sources, and event listeners. It describes various types of events and their corresponding listener interfaces, such as ActionEvent and MouseEvent, along with examples of handling mouse and keyboard events. Additionally, it introduces adapter classes, inner classes, and anonymous inner classes as methods to simplify event handling code.

Uploaded by

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

UNIT5-Part2-Java

The document explains event handling in Java, detailing the Delegation Event Model which consists of events, event sources, and event listeners. It describes various types of events and their corresponding listener interfaces, such as ActionEvent and MouseEvent, along with examples of handling mouse and keyboard events. Additionally, it introduces adapter classes, inner classes, and anonymous inner classes as methods to simplify event handling code.

Uploaded by

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

Event Handling

An event is one of the most important concepts in Java.


The change in the state of an object or behavior by performing actions is referred to as an Event in Java.

Example of events :Clicking a button,


Clicking a mouse and Dragging a mouse

A mechanism for controlling the events and deciding what should happen after an event occur is referred to as event handling. Java follows the
Delegation Event Model for handling the events.

Delegation Event Model


It consists of three components:

Events
Events Sources
Events Listeners

Events : Event is nothing but Object, It performs a special task.

Example of events : Clicking a button,


Clicking a mouse and Dragging a mouse

Event Sources
A source is an object that causes and generates events and sends to the Listener. The sources are allowed to generate several different types of
events.

A source must register a listener to receive notifications for a specific event. Each event contains its registration method. Below is an example:

public void addTypeListener (TypeListener e1)

From the above syntax, the Type is the name of the event, and e1 is a reference to the event listener. For example, for a keyboard event
listener, the method will be called as addKeyListener().

Event Listeners

An event listener is an object that is invoked when an event triggers. The listeners require two things; first, it must be registered with a source;
however, it can be registered with several resources to receive notification about the events. Second, it must implement the methods to
receive and process the received notifications.

Types of Events and Listeners

S.No. Event Class Listener Interface Methods Descriptions

1. ActionEvent ActionListener actionPerformed() ActionEvent will be


occurred when we
click a Button.

2. AdjustmentEvent AdjustmentListener adjustmentValueChanged() Adjustment events


occur by an
adjustable object like
a scrollbar.

3. ComponentEvent ComponentListener componentResized(), An event occurs


componentMoved(), when a component
componentShown() and moved, changed its
componentHidden() visibility or the size
changed.

4. ItemEvent ItemListener itemStateChanged() Item event occurs


when an item is
selected.

5. KeyEvent KeyListener keyPressed(), keyReleased(), and A key event occurs


keyTyped(). when the user
presses a key on the
keyboard.

6. MouseEvent MouseListener and mouseClicked(), mousePressed(), A mouse event


MouseMotionListener mouseEntered(), mouseExited() and occurs when the user
mouseReleased() are the interacts with the
mouseListener methods. mouse.
mouseDregged() and
mouseMoved() are the
MouseMotionListener() methods.

7. WindowEvent WindowListener windowActivated(), Window events


windowDeactivated(), occur when a
windowOpened(), windowClosed(), window's status is
windowClosing(), windowIconfied() changed.
and windowDeiconified().

Handling Mouse Events

To handle mouse events, we must implement the MouseListener and the MouseMotionListener interfaces and provide the implementations of
the below events.

MOUSE_CLICKED The user clicked the mouse.


MOUSE_DRAGGED The user dragged the mouse.
MOUSE_ENTERED The mouse entered a component.
MOUSE_EXITED The mouse exited from a component.
MOUSE_MOVED The mouse moved.
MOUSE_PRESSED The mouse was pressed.
MOUSE_RELEASED The mouse was released.
Program for handling mouse events :
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/*<applet code="MouseDemo" width=900 height=900>


</applet>*/
public class MouseDemo extends Applet implements MouseListener, MouseMotionListener {
int mx = 0;
int my = 0;
String msg = "";

public void init() {


addMouseListener(this);
addMouseMotionListener(this);
}

public void mouseClicked(MouseEvent me) {


mx = 20;
my = 40;
msg = "Mouse Clicked";
repaint();
}

public void mousePressed(MouseEvent me) {


mx = 30;
my = 60;
msg = "Mouse Pressed";
repaint();
}

public void mouseReleased(MouseEvent me) {


mx = 30;
my = 60;
msg = "Mouse Released";
repaint();
}
public void mouseEntered(MouseEvent me) {
mx = 40;
my = 80;
msg = "Mouse Entered";
repaint();
}

public void mouseExited(MouseEvent me) {


mx = 40;
my = 80;
msg = "Mouse Exited";
repaint();
}

public void mouseDragged(MouseEvent me) {


mx = me.getX();
my = me.getY();
showStatus("Currently mouse dragged " + mx + " " + my);
repaint();
}

public void mouseMoved(MouseEvent me) {


mx = me.getX();
my = me.getY();
showStatus("Currently mouse is moving at " + mx + " " + my);
repaint();
}
public void paint(Graphics g) {
g.drawString(msg, 60, 40);
}
}

Handling Keyboard Events

To handle keyboard events we must implements KeyListener interface When a key is pressed, a KEY_PRESSED event is generated and the
keyPressed( ) handler is executed. When the key is released, a KEY_RELEASED event is generated and the keyReleased( ) handler is executed. If
a character is generated by the keystroke, then a KEY_TYPED event is sent and the keyTyped( ) handler is invoked.

Program for handling KeyEvents

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

/*<applet code="KeyDemo" width=300 height=300>


</applet>*/
public class KeyDemo extends Applet implements KeyListener {
int mx = 40;
int my = 60;
String msg = "";

public void init() {


addKeyListener(this);
}
public void keyPressed(KeyEvent me) {
msg = "key Pressed";
repaint();
}

public void keyReleased(KeyEvent me) {


msg = "key Released";
repaint();
}

public void keyTyped(KeyEvent me) {


msg = "key Typed";
repaint();
}

public void paint(Graphics g) {


g.drawString(msg, mx,my);
}
}

Adapter Classes

Java provides a special feature, called an adapter class, that can simplify the creation of event handlers in certain situations.
An adapter class provides an empty implementation of all methods in an event listener interface.
Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener
interface.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="AdapterDemo" width=300 height=100>
</applet>*/

public class AdapterDemo extends Applet {


public void init() {
addKeyListener(new MyKeyAdapter(this));
}
}
class MyKeyAdapter extends KeyAdapter {
AdapterDemo a1;
public MyKeyAdapter(AdapterDemo a) {
a1=a;
}

public void keyReleaed(KeyEvent me) {


a1.showStatus("Key Released");
}
}

Inner Classes

It simplifies the event handler code instead of maintaining two classes.


InnerClassDemo is a top-level class that extends Applet. MyMouseAdapter is an inner class that extends MouseAdapter. Because
MyMouseAdapter is defined within the scope of InnerClassDemo, it has access to all of the variables and methods within the scope of that
class. Therefore, the keyReleased( ) method can call the showStatus( ) method directly.

import java.applet.*;
import java.awt.event.*;
/*
<applet code="InnerClassDemo" width=200 height=100>
</applet>
*/
public class InnerClassDemo extends Applet {
public void init() {
addKeyListener(new MyKeyAdapter());
}
class MyKeyAdapter extends MouseAdapter {
public void keyReleased(KeyEvent me) {
showStatus("Key is Released");
}
}
}

Anonymous Inner Classes

An anonymous inner class is one that is not assigned a name. below program describes how an anonymous inner class can facilitate the writing
of event handlers.

import java.applet.*;
import java.awt.event.*;
/*<applet code="AnonymousInnerClassDemo" width=200 height=100>
</applet>
*/
public class AnonymousInnerClassDemo extends Applet {
public void init() {
addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent me) {
showStatus("Key is Released");
}
});

You might also like