Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
272 views

Event Handling in Java

The document discusses Java event handling and describes event sources, event listeners, event handlers, common event classes like ActionEvent and MouseEvent, and event listener interfaces like ActionListener and MouseListener.

Uploaded by

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

Event Handling in Java

The document discusses Java event handling and describes event sources, event listeners, event handlers, common event classes like ActionEvent and MouseEvent, and event listener interfaces like ActionListener and MouseListener.

Uploaded by

Prashant Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Handling Events

 GUI provides an interface through which a user


interacts with the application. Therefore, it is
necessary that the application must respond to the
actions performed by the user.

 The user’s actions are represented through events


in the Java programming language. To handle these
events, Java provides a mechanism known as event
model.

 An event is handled by defining and associating a


handler for the event.

 Defining a handler enables the application to


respond to the event, and associating a handler
enables the application to bind the handler with the
event.
Exploring Java Event Model
 In a GUI application, a user interacts with User
Interface (UI) in various ways, such as by clicking a
button, moving a mouse, entering text in a text field,
and selecting an item from a list.

 Moreover, the interaction can also be generated by


the application itself, such as when a timer expires,
the printing process of a document completes, and
some hardware or software failure occurs. The
interaction with the application is incorporated in
Java programs by using the event model.

 The event model is based on two entities, source and


receiver.

 The source initiates the interaction and the receiver


processes the interaction generated by the source.
In Java, events are handled by the classes and
interfaces defined in the java.awt.event package.
Introducing Event Model
In Java programs, the event model contains the
following three components that are responsible
for handling the interaction between the source
and the receiver:

 Event source

 Event listener

 Event handler
Event Source
 An event source is an object that generates an event.
The object registers some handler, which receives
notifications about a specific type of event.
For example,
clicking of a button is an event that is fired by the
button. In this example, the button is the source of the
event and the button-click-event is the notification
that is sent to the handler.

 There are various components in the Swing toolkit,


such as button, frame, and combo box, which act as
an event source.

 These sources generate various events, such as a


button generates an action event when the button is
clicked, a frame generates the window event when
the window is opened or closed, and a combo box
generates the item event when an item is selected or
deselected.
Event Listener
 An event listener listens for a specific event
and gets notified when the specific event
occurs.

 An event listener registers with one or more


event sources to receive notifications about
specific types of events and to process the
events.

 Once the listener is notified of a specific


event, it calls the event handler and passes
the information regarding the event.

 The information may contain the source that


generated the event and the time when the
event was generated.
Event Handler
 An event handler is a method that is invoked
by an event listener when the event is
generated.

 In Java, event listeners are the interfaces


and event handlers are the methods declared
in the event listener interface that is
implemented by the application.
Introducing Event Classes
Event classes represent the characteristics of all events,
such as the source, time, and ID of the event. Whenever
an event occurs, the related object of the event class is
created.

For example: when the mouse button is clicked, an


object of the MouseEvent class is created. Some of the
event classes provided by Java are:

 ActionEvent

 KeyEvent

 MouseEvent

 FocusEvent

 ItemEvent

 WindowEvent
The EventObject class, which is in the java.util
package, is the superclass for all event classes. The
class, EventObject, from which all event objects are
derived, extends the Object class and implements the
Serializable interface.

The syntax of one of the constructors in the


EventObject class is:

public EventObject(Object source)

In the preceding syntax, source is an object that


generates an event. The EventObject class contains
two methods, the getSource() method and the
toString() method. The getSource() method returns
the object on which an event has actually occurred.

The toString() method returns the string


representation of the object involved in the event.
ActionEvent
The action event is generated by components, such as a
button, when an action is performed.
For example, clicking of a button is an action event
generated by the button.

The following table lists the most commonly used


methods of the ActionEvent class.
MouseEvent
The mouse event is generated by a mouse with respect to
the source component. These mouse events can have the
following two categories:
 Mouse events: It contains the mouse events, such as
mouse pressed, mouse clicked, and mouse entered.
 Mouse motion events: It contains the mouse motion
events, such as mouse moved and mouse dragged.

The following table lists the most commonly used methods


of the MouseEvent class.
KeyEvent
The key event is generated by a component when a key
is pressed, released, or typed within the component.

The following table lists the most commonly used


methods of the KeyEvent class.
FocusEvent
The focus event is generated by a component when it
receives or loses focus. The focus events have the
following levels:

Permanent: It occurs when the focus directly moves


from one component to other. For example, when you
use the TAB key or the mouse to traverse from one
component to other.

Temporary: It occurs when the focus is temporarily


lost for a component as an indirect result of another
operation. For example, when you open a frame by
clicking a button, the focus shifts temporarily from
the button to the frame. When you close the frame,
the focus shifts back from the frame to the button.

The following table lists the most commonly used


methods of the FocusEvent class.
FocusEvent
ItemEvent
The item event is generated by components, such as
radio button, and check box, when an item or the
component is selected or deselected by the user.

The following table lists the most commonly used


methods of the ItemEvent class.
WindowEvent
The window event is generated by window when it is
opened, closed, activated, deactivated, or when focus
is transferred into or out of the window.

The following table lists the most commonly used


methods of the WindowEvent class.
Introducing Event Listeners
An event listener provides the event handler methods
that are used to handle events.
For example,
when a button is clicked, the actionPerformed()
method of the ActionListener interface is responsible
for handling the button-click event. Some of the event
listener interfaces provided by Java are:

 ActionListener

 KeyListener

 MouseListener

 MouseMotionListener

 FocusListener

 ItemListener

 WindowListener
ActionListener
The ActionListener interface handles the action event.
It is implemented by the class that processes the
action events.

The following table lists the method provided by the


ActionListener interface to handle the specific
events.
KeyListener
The KeyListener interface handles the keyboard
events generated by keystrokes. The class needs to
implement the KeyListener interface that handles the
keyboard events.

The following table lists the methods provided by the


KeyListener interface to handle the specific events.
MouseListener
The MouseListener interface handles the mouse
events that occur when the mouse pointer is within the
bounds of the component.

The following table lists the methods provided by the


MouseListener interface to handle the specific events.
MouseMotionListener
The MouseMotionListener interface handles the
events that occur on the mouse movement with
respect to the component.

The following table lists the methods provided by the


MouseMotionListener interface to handle the specific
events.
FocusListener
The FocusListener interface handles the events that
occur when the component gains or loses the focus.
The following table lists the methods provided by the
FocusListener interface to handle the specific events.
ItemListener
The ItemListener interface handles the events
generated by the selectable component, such as combo
box and radio button.

The following table lists the method provided by the


ItemListener interface to handle the specific events.
WindowListener
The WindowListener interface handles the events
generated by the window.

The following table lists the methods provided by the


WindowListener interface to handle specific events.

You might also like