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

Java Event Handling

The document discusses event handling in Java. It defines what an event is and different types of events. It describes event handling and its components including events, event sources, and listeners. It provides examples of mouse, key, and action listeners.

Uploaded by

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

Java Event Handling

The document discusses event handling in Java. It defines what an event is and different types of events. It describes event handling and its components including events, event sources, and listeners. It provides examples of mouse, key, and action listeners.

Uploaded by

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

Java Event Handling

What is an Event?
Change in the state of an object is known as event i.e. event
describes the change in state of source. Events are generated as
result of user interaction with the graphical user interface
components. For example, clicking on a button, moving the
mouse, entering a character through keyboard, selecting an item
from list, scrolling the page are the activities that causes an event
to happen.
In other words
Any program that utilizes GUI (graphic user interface) like
windows-written Java application is event oriented. Event defines
any object's change in status. For example: press a button, enter
a character in textbox, click or drag a mouse, etc.

Types of Event
The events can be broadly classified into two categories:
 Foreground Events - Those events which require the direct
interaction of user. They are generated as consequences of
a person interacting with the graphical components in
Graphical User Interface. For example, clicking on a button,
moving the mouse, entering a character through keyboard,
selecting an item from list, scrolling the page etc.
 Background Events - Those events that require the indirect
interaction of end user are known as background events.
Operating system interrupts, hardware or software failure,
timer expires, an operation completion are the example of
background events.
What is Event Handling?
Event Handling is the mechanism that controls the event and
decides what should happen if an event occurs. This mechanism
has the code which is known as event handler that is executed
when an event occurs.
Java Uses the Delegation Event Model to handle the events.
This model defines the standard mechanism to generate and
handle the events.

Components of Event Handling

Event handling has three main components,

 Events: An event is a change in state of an object.



 Events Source: An event source is an object that generates
an event. 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.

 Listeners: A listener is an object that listens to the event. A


listener gets notified when an event occurs. 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.
// Mouse Listener
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <applet code="MListen.class" width=500 height=500>
</applet> */
public class MListen extends Applet implements MouseListener
{
int x,y;
public void init()
{
addMouseListener(this);
}
public void mouseClicked(MouseEvent ev)
{
x=ev.getX();
y=ev.getY();
showStatus("Mouse is on "+x+" & "+y);
}
public void mouseExited(MouseEvent ev)
{}
public void mouseEntered(MouseEvent ev)
{}
public void mousePressed(MouseEvent ev)
{}
public void mouseReleased(MouseEvent ev)
{}
}

// Mouse Motion Listener

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <applet code="MListen1.class" width=500 height=500>
</applet> */

public class MListen1 extends Applet implements


MouseMotionListener
{
int x,y;
public void init()
{
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent ev)
{
x=ev.getX();
y=ev.getY();
showStatus("Mouse is on "+x+" & "+y);
}

public void mouseDragged(MouseEvent ev)


{}
}

// Key Listener

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="KListen.class" width=500 height=500>
</applet> */

public class KListen extends Applet implements KeyListener


{
String msg="";
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyPressed");
}
public void keyReleased(KeyEvent k)
{
showStatus("KeyRealesed");
}
public void keyTyped(KeyEvent k)
{
msg = msg+k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, 20, 40);
}
}
// Action Listener

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

/* <applet code="apevent.java" height=500 width=500>


</applet> */

public class apevent extends Applet implements ActionListener


{
Button b = new Button("click here");
public void init()
{
add(b);
b.addActionListener(this);
}
public void paint(Graphics g)
{
g.drawLine(100,100,200,200);
}
public void actionPerformed(ActionEvent ae)
{
setBackground(Color.blue);
}
}

You might also like