1. What is 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.
2. Types of Event
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 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.
3. What is Event Handling?
Event Handling is the mechanism that
controls the event and decides what
should happen if an event occurs.
This mechanism have 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.
Let's have a brief introduction to this
model.
4. The Delegation Event Model has the
following key participants namely:
Source -
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.
Listener -
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.
5. Steps involved in event handling
The User clicks the button and the event
is generated.
Now the object of concerned event class
is created automatically and information
about the source and the event get
populated with in same object.
Event object is forwarded to the method
of registered listener class.
the method is now get executed and
returns.
6.
7.
8.
9.
10. 1.Action Keyword
This interface is used for receiving the
action event.
This interface defines the actionPerformed()
method that is invoked when an action
event occurs.
Method:
void actionPerformed(ActionEvent e)
19. 3.KeyEvent class
This interface is used for receiving the key events.
Methods:
public void keyPressed(KeyEvent e)
The "key pressed" event. This event is generated
when a key is pushed down.
public void keyReleased(KeyEvent e)
The "key released" event. This event is generated
when a key is let up.
public void keyTyped(KeyEvent e)
The "key typed" event. This event is generated when
a character is entered.
20.
21.
22.
23.
24.
25.
26.
27.
28. 4.Mouse Listener
This interface is used for receiving the mouse event.
Methods:
public void mouseClicked(MouseEvent e) {…}
Called just after the user clicks the listened-to component.
public void mousePressed(MouseEvent e) {….}
Called just after the user presses a mouse button while the cursor is over
the listened-to component.
public void mouseReleased(MouseEvent e) {...}
Called just after the user releases a mouse button after a mouse press
over the listened-to component
public void mouseEntered(MouseEvent e) {…….}
Called just after the cursor enters the bounds of the listened-to
component.
public void mouseExited(MouseEvent e) {……..}
Called just after the cursor exits the bounds of the listened-to
component.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39. 5.TextListener interface
The listener interface for receiving text
events.
Method:
public void textValueChanged(TextEvent e)
Invoked when the value of the text has
changed.
The code written for this method performs
the operations that need to occur when text
changes.
40.
41. 6.WindowListener interface
This interface is used for receiving the window events.
Method:
void windowActivated(WindowEvent e)
◦ Invoked when the Window is set to be the active Window.
void windowDeactivated(WindowEvent e)
◦ Invoked when a Window is no longer the active Window.
void windowClosed(WindowEvent e)
◦ Invoked when a window has been closed as the result of calling dispose
on the window
void windowClosing(WindowEvent e)
◦ Invoked when the user attempts to close the window from the window's
system menu.
void windowIconified(WindowEvent e)
◦ Invoked when a window is changed from a normal to a minimized state.
For many platforms, a minimized window is displayed as the icon
specified in the window's iconImage property.
void windowDeiconified(WindowEvent e)
◦ Invoked when a window is changed from a minimized to a normal state.
void windowOpened(WindowEvent e)
◦ Invoked the first time a window is made visible.
42.
43. 7.ContainerListener interface
The listener interface is used for receiving
container events.
Method:
void componentAdded(ContainerEvent e)
◦ Invoked when a component has been added
to the container.
void componentRemoved (ContainerEvent e)
◦ Invoked when a component has been
removed from the container.
44.
45. 8.MouseMotion Listener Interface
This interface is used for receiving mouse
motion events.
Methods:
public void mouseDragged(MouseEvent e)
• It invokes when a mouse button is pressed on a
component and then dragged.
public void mouseMoved(MouseEvent e)
• It invokes when a mouse cursor has been
moved on to a component but no button has
been pushed.
51. 9.Focus Listener Interface
This interface is used for receiving the focus
events methods.
Methods:
public void focusGained(FocusEvent e)
• It invokes when a component gains the keyboard
focus.
public void focusLost(FocusEvent e)
• It invokes when a component loss the keyboard
focus.
52.
53.
54.
55.
56.
57. Adapter Class
Adapters are used to replace the listeners.
Adapter make event handling easy to the programmer.
Every listener that includes more than one abstract
method has got a corresponding adapter class.
The advantage of this adapter is that we can override
any one or two methods like instead of all.
In case of a listener, we must override all the abstract
methods.
58. Example:
The MouseListener interface has five
methods: mouseClicked(), mouseEntered(),
mouseExited(), mousePressed() and
mouseReleased().
If in your program, you just need two
events: mouseEntered() and
mouseExited() that time you can use
adapter class for the mouseListener
interface.
The adpter classes contain an empty
implementation for each method of the event
listener interface.
To use the adapter class, you have to extend
that adapter class.
62. Inner Class
Inner classes are class within class.
Inner class has special relationship with other class.
The special relationship gives inner class access to
method of outer class as if they are the part of outer
class.
Inner class instants or object has access to all method of
outer class (public, private and protected).
As with instants methods and variables, an inner class is
associated with an instants of its enclosing class and has
direct access to that object methods and field also an
inner class is associated with an instances it can not
define any static members itself.
64. Example
Import java.io.*;
public class outer
{
int i=10;
public outer();
{
new inner();
System.out.println(“Inside outer class”);
}
Class inner
{
public inner()
{
System.out.println(“Inside inner class”);
System.out.print(“Value of outer class variable i=“+i);
}
}
public static void main(String [] args)
{
New outer();
}
}