0% found this document useful (0 votes)
41 views6 pages

Important Event Classes and Interface

Uploaded by

madani hussain
The document discusses event handling in GUI programs, explaining that events are generated by event sources and handled by registered event listeners. It describes the key components of events, event sources, and listeners. It also covers the delegation event model, common event classes, how to handle events, using adapter classes to handle specific event types, and implementing inner classes to handle events.

Copyright:

© All Rights Reserved

Available Formats

Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
41 views6 pages

Important Event Classes and Interface

Uploaded by

madani hussain
The document discusses event handling in GUI programs, explaining that events are generated by event sources and handled by registered event listeners. It describes the key components of events, event sources, and listeners. It also covers the delegation event model, common event classes, how to handle events, using adapter classes to handle specific event types, and implementing inner classes to handle events.

Copyright:

© All Rights Reserved

Available Formats

Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

unit-5

event handling

Any program that uses GUI (graphical user interface) such as Java application written
for windows, is event driven. Event describes the change in state of any object. For
Example : Pressing a button, Entering a character in Textbox, Clicking or Dragging a
mouse, etc.
Event handling has three main components,

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


 Events Source : Event source is an object that generates an event.
 Listeners : A listener is an object that listens to the event. A listener gets
notified when an event occurs.

A source generates an Event and send it to one or more listeners registered with the
source. Once event is received by the listener, they process the event and then return.
Events are supported by a number of Java packages,
like java.util, java.awt and java.awt.event.

Important Event Classes and Interface


Steps to handle events:

1. Implement appropriate interface in the class.


2. Register the component with the listener.

program:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/* <applet code="Test.class " width=100 height=100>
</applet> */
public class Test 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);
}
}

delegation event model:


The modern approach to handling events is based on the delegation event model,
which defines standard and consistent mechanisms to generate and process events. Its
concept is quite simple: a source generates an event and sends it to one or more
listeners. In this scheme, the listener simply waits until it receives an event. Once an
event is received, the listener processes the event and then returns. The advantage of
this design is that the application logic that processes events is cleanly separated from
the user interface logic that generates those events. A user interface element is able to
“delegate” the processing of an event to a separate piece of code.
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.
Event:
In the delegation model, an event is an object that describes a state change in a source.
It can be generated as a consequence of a person interacting with the elements in a
graphical user interface. Some of the activities that cause events to be generated are
pressing a button, entering a character via the keyboard, selecting an item in a list, and
clicking the mouse.

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.
A source must register listeners in order for the listeners to receive notifications about
a specific type of event. Each type of event has its own registration method. Here is
the general form:
public void addTypeListener(TypeListener el)
Here, Type is the name of the event, and el is a reference to the event listener. For
example, the method that registers a keyboard event listener is called addKeyListener(
). The method that registers a mouse motion listener is called
addMouseMotionListener( ).
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 events. Second, it must implement methods to
receive and process these notifications..

Adapter classes
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. You can define a
new class to act as an event listener by extending one of the adapter classes and
implementing only those events in which you are interested.
For example, the MouseMotionAdapter class has two methods, mouseDragged( ) and
mouseMoved( ), which 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 of mouseMoved( ) would handle the mouse motion events for you.

program:
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() {
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter {
AdapterDemo adapterDemo;
public MyMouseAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
// Handle mouse clicked.
public void mouseClicked(MouseEvent me) {
adapterDemo.showStatus("Mouse clicked");
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter {
AdapterDemo adapterDemo;
public MyMouseMotionAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
// Handle mouse dragged.
public void mouseDragged(MouseEvent me) {
adapterDemo.showStatus("Mouse dragged");
}
}
Inner classes:

Java inner class or nested class is a class which is declared inside the class or
interface.

We use inner classes to logically group classes and interfaces in one place so that it
can be more readable and maintainable.

Additionally, it can access all the members of outer class including private data
members and methods

There are basically three advantages of inner classes in java. They are as follows:

1) Nested classes represent a special type of relationship that is it can access all the
members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable
code because it logically group classes and interfaces in one place only.

3) Code Optimization: It requires less code to write.

import java.applet.*;
import java.awt.event.*;
/*
<applet code="InnerClassDemo" width=200 height=100>
</applet>
*/
public class InnerClassDemo extends Applet {
public void init() {
addMouseListener(new MyMouseAdapter());
}
class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent me) {
showStatus("Mouse Pressed");
}
}
}

You might also like