Event Handling
Event Handling
1
To implement Graphical User Interface programming, the event
handling is very important.
Working:
2
Visual Basic Approach:
Each GUI component response to a fixed set of events.
We can not change in it.
C Language Approach:
The EVENT QUEUE is not hidden from programmer.
Event Source:
Event Class:
4
Event Delegation Model:
Event Source
Cr
ea
ter
tes
gis
Re
Event Listener Sends Event Class
Interface Object
In
vo
kes
Event Listener
Methods
5
Event Delegation Model:
6
Different Events by different Event Sources:
When an event occurs an Event Source sends the event object to all the
registered Listeners.
7
Register an Event Source to Listener: -
eventSourceObject.addEventListener (eventListenerObject);
e.g.
8
Respond to Event: -
First we need to write definition of class that implements Listener
Interface.
EventObject
|
AWTEvent
_________________________|________________________________
| | | |
ActionEvent Adjustment Event Component Event
ItemEvent _________________________________|
______________________
| | | |
FocusEvent InputEvent PaintEvent WindowEvent
___________|__________________
| |
KeyEvent MouseEvent
|
MouseWheelEvent
10
Some Commonly used AWT Event Classes: -
ActionEvent KeyEvent
AdjustmentEvent MouseEvent
FocusEvent MouseWheelEvent
ItemEvent WindowEvent
11
Assignment:
Create Frame with three Button Red, Blue, and Green in it.
Write a program to handle the Action Event such that
When we click on Red Button, Background Color of frame changes to
Red, if click on Blue Button, then Background Color of frame changes to
Blue, and so on.
12
When single panel sets as listener to more than one event source,
then we need to identify the event source which generates the event.
e.g.
In above assignment all three buttons have single listener panel.
So
In actionPerformed() method we need to identify the button for which
it get called, to performed appropriate action.
1)
String getActionCommand(): - This method returns the command string
associated with the event source.
2)
Object getSource(): - This method returns the event source for which
actionPerformed() method get called.
13
e.g. 1)
Note: - We may make an easy mistake to caption the button and then spell
the string differently while checking with command string.
14
e.g. 2.
if( src = = b1 )
----
else if( src = = b2 )
----
else if( src = = b3 )
----
}
15
Window Events: -
To handle the Window events we need to used WindowEvent class and
WindowListener interface. Window event is performed on Window
and not on Panel.
16
The 7 methods of WindowListener are…
public void windowOpened(WindowEvent we)
// get called after window has been opened.
Otherwise…
18
Adapter Classes: -
It is tedious task to write the definition of six do nothing methods.
To simplify this task, AWT provides us Adapter Classes for all the
Interfaces which having more than one method in it.
Note: - But we can not extends our frame class from two classes i.e. Frame
and WindowAdapter i.e. Multiple Inheritance is not allowed.
19
So we need to extends new class from Adapter class as…
And then…
FrameEvent fevt = new FrameEvent();
frm.addWindowListener(fevt);
20
So, we can write directly as…
Anonymous class: -
It is class, which has no name.
21
Rules:
An anonymous class must always extend a super class or implement an
interface but it cannot have an explicit extends or implements clause.
An anonymous class always uses the default constructor from the super
class to create an instance.
e.g.
frm.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
22
This will do…
Creates an object of anonymous class, that also does not have a name.
23
Mouse Events: -
When the user clicks a mouse button, three methods get called for
MouseListener…
24
When the user moves a mouse,
two methods get called for MouseMotionListener…
25
Some methods used to handle mouse event…
26
Key Events: -
When user presses any key on keyboard this event occur. We need
to handle the key events.
Java makes an distinction between characters on the Keyboard and virtual key
(functional keys) codes.
Virtual key codes are indicated with a prefix of VK_ such as…
VK_A - denotes the key marked with A,
VK_B - denotes the key marked with B,
VK_SHIFT - denotes the key marked with Shift,
and so on…
27
Note: - There is no separate lowercase virtual key code.
e.g.
If user types an uppercase “A”, i.e. Shift + a, then 5 events generated are…
Pressed the SHIFT key - keyPressed() get called for VK-SHIFT.
If user types an lowercase “a”, i.e. just A, then 3 events generated are…
Pressed the A key - keyPressed() get called for VK_A.
Typed “a” key - keyTyped() get called for an “a”.
Released the “a” key - keyReleased() get called for VK_A.
28
While working with keyPressed and keyReleased methods…
29
While working with keyTyped method…
The current state of SHIFT, CONTROL, ALT keys can be checked as…
30
Following code will tests whether user presses
SHIFT + RIGHT ARROW
31