Event Handling in Java
Table of Contents
a) Introduction to Event
b) What is Event Handling?
c) Event Delegation Model (EDM)
d) Event Listener and its Description
e) Event Class and its Description
Introduction
Now after the Layout Managers, in this chapter we will learn about Event, Event types, and also learn to handle the Event. You have noticed that in our previous examples I have added the buttons but that button does not perform any kind of activity. So, to make the GUI interactive Event should be handled. It also helps for making easy user interface.What is Event Handling?
Event handling is the mechanism of controlling the events and decide what to do if an event occurs, Each and every time if an event occurs then the set of code executes ( which is called Event Handler ) and performs the things as per the set of codes. Event is handled by using the Event Delegation Model (EDM). Before moving forward let us learn about the Event Delegation Model (EDM). It consists of: Event Source, Event and Event Listener.
Event Source:
Event source is the source from which the event is generated. It means that if
a button is clicked than the button is the event source and so on. There are
different source for generating events and we will discuss further in the other
chapters.
Event: Event is the change in the state of an
object. Events are generated through event source which means if a user wants
to interact with the Graphical User Interface (GUI) then the user may click to
the button and after he clicks the button, action event is generated. And also
there are Key Event, Mouse Event, Window Event etc and all these will be
discussed in further classes.
Event Listener:Â
It performs the action after the getting the command of the event.
According to the event, the event listener does the rest things. For every
event listener there is its corresponding method that is used.
Now let us know about the Event Class and Its Descriptions;
Event lListener
|
Description
|
ActionEvent
|
Generated when a
button is pressed, a list item is double clicked, a menu item is selected,
enter key is pressed in a text field,
item in combo box is clicked
|
AdjustmentEvent
|
Generated when a
scroll bar is manipulated
|
ComponentEvent
|
Generated when a
component is hidden, moved, resized or shown
|
ContainerEvent
|
Generated when a
component is added to/ removed from a container
|
FocusEvent
|
Generated when a
component gains or loses keyboard focus
|
ItemEvent
|
Generated when a
check box , list item or combo box
item is clicked.
|
KeyEvent
|
Generated when input
is received from the keyboard
|
MouseEvent
|
Generated when mouse
is clicked, pressed, or released. Also generated when a mouse enters or exits
a component
|
MouseWheelEvent
|
Generated when the
mouse wheel is moved
|
TextEvent
|
Generated when the
value of text area or text field is changed
|
WindowEvent
|
Generated when a
window is activated, closed, deactivated, deiconified, iconified, opened or
quit.
|
The Adapter class and Listener Interface used in event handling are;
Adapter Classes
|
Listener Interfaces
with their methods
|
ComponentAdapter
|
ComponentListener
void
componentResized(ComponentEvent e)
void
componentMoved(ComponentEvent e)
void
componentShown(ComponentEvent e)
void
componentHidden(ComponentEvent e)
|
ContainerAdapter
|
ContainerListener
void
componentAdded(ContainerEvent e)
void
componentRemoved(ContainerEvent e)
|
FocusAdapter
|
FocusListener
void
focusGained(FocusEvent e)
void
focusLost(FocusEvent e)
|
KeyAdapter
|
KeyListener
void
keyPressed(KeyEvent e)
void
keyReleased(KeyEvent e)
void
keyTyped(KeyEvent e)
|
MouseAdapter
|
MouseListener
void
mouseClicked(MouseEvent e)
void
mouseEntered(MouseEvent e)
void
mouseExited(MouseEvent e)
void
mousePressed(MouseEvent e)
void mouseReleased(MouseEvent
e)
|
MouseMotionAdapter
|
MouseMotionListener
void
mouseDragged(MouseEvent e)
void
mouseMoved(MouseEvent e)
|
WindowAdapter
|
WindowListener
void
windowActivated(WindowEvent e)
void
windowClosed(WindowEvent e)
void
windowClosing(WindowEvent e)
void
windowDeactivated(WindowEvent e)
void
windowDeiconified(WindowEvent e)
void
windowIconified(WindowEvent e)
void
windowOpened(WindowEvent e)
|
         x
|
ActionListener
void
actionPerformed(ActionEvent e)
|
         x
|
ItemListener
void itemStateChanged(ItemEvent
e)
|
        x
|
AdjustmentListener
void
adjustmentValueChanged(AdjustmentEvent e)
|
        x
|
TextListener
void
textChanged(TextEvent e)
|
Advantage of using Adapter Class over Listener Interface is that we can override only those method which is required to override and do not take the burden of other methods.
[ Note : The ‘x’ sign represent that it does not have Adapter class because they have only one method.]
This chapter includes the theoretical description of the Event Handling. In the next chapter we will discuss further about the event handling with examples.