Sei sulla pagina 1di 34

EVENT HANDLING

SHYNA
What is an Event?
GUI components communicate with the rest of the applications
through events.
EVENT: an object that describes the state change in a source.
Eg: Clicking on a button,Moving the mouse,Selecting an item
from the list
EVENT HANDLING
A mechanism that controls the events and decides what should
happen if an event occurs.
Delegation Event Model
• model defines the standard mechanism to
generate and handle the events
• key participants are
• Event
• Event Source
• Event Listner
• Ev

09-05-2018 RSET
The source of an event is the component that causes that event to
occur.-EVENT SOURCE
• Source is responsible for providing information of the occurred
event to it's handler.

The listener (Event Handler)of an event is an object that receives the


event and processes it appropriately.-EVENT LISTENER

09-05-2018 RSET
To be notified for an event,

 The object has to be registered as an event listener on the


appropriate event source.

 The object has to implement the appropriate interface.

09-05-2018 RSET
Advantages:user interface logic is completely
separated from the logic that generates the event.
The user interface element is able to delegate the
processing of an event to the separate piece of
code.

09-05-2018 RSET
The Event Handling process
1. The user performs an action, causing an event to be triggered
(or fired).
2. An object is created that contains information about the
event, including an indication of which component was
involved
3. A method that belongs to a listener object is called. The object
created in step 2 is passed to the method.

The java.awt.event package provides many event


classes and Listener interfaces for event handling.
The Event Handling Process(contd..)
Event Class
• When an event occurs, an object is created that contains
information about the event.

• This object will belong to one of several different


classes, depending on the nature of the event.

• These classes all belong to the java.awt.event package.

09-05-2018 RSET
The Event classes
An event object has an event class as its reference data type.
The Event object class
 Defined in the java.util package.
The AWT Event class
 An immediate subclass of EventObject.
 Defined in java.awt package.
 Root of all AWT based events.
Hierarchy of event objects

Note: The number


of event objects is
much greater then
specified in
diagram…Due to
space constraints,
only some of them
are represented in
the figure

Courtesy: Safari.oreilly.com
Events
High Level Events
Class Name Description of Event
ActionEvent A significant action has been performed on
a component (a button was pressed, a list
item was double-clicked, or the Enter key
was pressed in a text field).
AdjustmentEvent The state of an adjustable component (such
as a scrollbar) has changed.
ItemEvent An item has been selected (or deselected)
within a checkbox, choice menu, or list.
TextEvent The contents of a text area or text field
have changed.s
. 12
Event classes for GUI controls

low level event classes.


FocusEvent : This event is generated when a component gains or looses focus. Focus may be
gained by bringing the mouse over a component (or by using the tab key). The component
that has the focus receives all user keyboard events. Focus events are generated by objects
of Component class and all its subclasses.
KeyEvent and MouseEvent are subclasses of abstract InputEvent class. Both these events are
generated by objects of type Component class and its subclasses. The KeyEvent is generated
when the user presses or releases a key on the keyboard. The MouseEvent is generated
when the user presses the mouse or moves the mouse.
WindowEvent are generated for the Window class and its subclasses. These events are
generated if an operation is performed on a window. The operation could be closing of
window, opening of window, activating a window etc.

09-05-2018 RSET
Event classes for GUI controls

PaintEvent is generated when a component needs to be repainted (for example


when an application which is in front is closed and the component needs to
be redrawn.) PaintEvents are internally handled by AWT, and cannot (and
should not) be handled by you in the application.
ComponentEvent are also generated by objects of type Component class and its
subclasses. This event is generated when a component is hidden, shown,
moved or resized.
ContainerEvents are generated when a component is added or removed from a
container. ComponentEvent and ContainerEvent are handled by AWT and
are not normally handled by the use

09-05-2018 RSET
Types of Listener Interface

Act causing Event Listener Interface Type

User clicks a button, presses ActionListener


Enter, typing in text field
User closes a frame WindowListener

Clicking a mouse button, while MouseListener


the cursor is over a component
Types of Events (contd..)

Act causing Event Listener Type

User moving the mouse over a MouseMotionListener


component

Component becomes visible ComponentListener

Table or list selection changes ListSelectionListener


The ActionListener Interface

It contains exactly one method.

Example:
public void
actionPerformed(ActionEvent e)
The above code contains the handler for the ActionEvent e
that occurred.
The MouseListener interface
Event handling when the mouse is clicked.
public void mouseClicked(MouseEvent
e)
Event handling when the mouse enters a component.
public void mouseEntered(MouseEvent
e)
Event handling when the mouse exits a component.
public void mouseExited(MouseEvent
e)
The MouseListener Methods (contd..)

Event handling when the mouse button is pressed on a


component.
public void
mousePressed(MouseEvent e)
Event handling when the mouse button is released on a
component.
public void
mouseReleased(MouseEvent e)
The MouseMotionListener Methods

Invoked when the mouse button is pressed over a component and


dragged. Called several times as the mouse is dragged
public void mouseDragged(MouseEvent e)
Invoked when the mouse cursor has been moved onto a component
but no buttons have been pushed.
public void mouseMoved(MouseEvent e)
The WindowListener
Interface

Invoked when the window object is opened.


public void windowOpened(WindowEvent e)
Invoked when the user attempts to close the window object from the
object’s system menu.
public void windowClosing(WindowEvent e)
The WindowListener Methods (contd..)

Invoked when the window object is closed as a result of calling dispose


(release of resources used by the source).
public void windowClosed(WindowEvent e)
Invoked when the window is set to be the active window.
public void windowActivated(WindowEvent e)
The WindowListener Methods (contd..)

Invoked when the window object is no longer the active window


public void
windowDeactivated(WindowEvent e)
Invoked when the window is minimized.
public void
windowIconified(WindowEvent e)
Invoked when the window is changed from the minimized state to
the normal state.
public void
windowDeconified(WindowEvent e)
Additional Listener Types

Change Listener Item Listener


Container Listener Key Listener
Document Listener Property Change Listener
Focus Listener Table Model Listener
Internal Frame Listener
What does an Event Handler require?

It just looks for 3 pieces of code!


First, in the declaration of the event handler class, one line
of code must specify that the class implements either a
listener interface or extends a class that implements a
listener interface.
public class DemoClass implements
ActionListener {
What does an Event Handler require?(contd.)

Second, it looks for a line of code which registers an instance of


the event handler class as a listener of one or more
components because, as mentioned earlier, the object must be
registered as an event listener.
anyComponent.addActionListener(inst
anceOf DemoClass);
What does an Event Handler require?(contd.)

Third, the event handler must have a piece of code that


implements the methods in the listener interface.

public void
actionPerformed(ActionEvent e) {
...//code that reacts to the
action...
}
An example of Event Handling

public class AWTApplication implements ActionListener


{
...
Button button = new Button(“Click me");
button.addActionListener(this);
....
public void actionPerformed(ActionEvent e) {
String str= e.getActionCommand();
if(str.equals(“Click me”)) {
msg = “you pressed”;
}
}
}
If evt is an instance of any event class, the source of the event can be found
by calling getSource:
Object source = evt.getSource();
EG: if (source == testButton) …

The other way to determine the origin of a button press is to use the
getActionCommand method.
String label = evt.getActionCommand();
if (label.equals("Testing")) …
s

09-05-2018 RSET
Adapter classes for Event Handling.

Why do you need adapter classes?


 Implementing all the methods of an interface
involves a lot of work.
 If you are interested in only using some
methods of the interface.
Adapter classes
 Built-in in JAVA
 Implement all the methods of each listener
interface with more than one method.
 Implementation of all empty methods
Adapter classes – an Illustration.
Consider, you create a class that implements a MouseListener
interface, where you require only a couple of methods to be
implemented. If your class directly implements the
MouseListener, you must implement all five methods of
this interface.
Methods for those events you don't care about can have empty
bodies
Illustration (contd..)

public class MyClass implements MouseListener {


... someObject.addMouseListener(this);
/* Empty method definition. */
public void mousePressed(MouseEvent e) { }
/* Empty method definition. */
public void mouseReleased(MouseEvent e) { }
/* Empty method definition. */
public void mouseEntered(MouseEvent e) { }
/* Empty method definition. */
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {
//Event listener implementation goes here...
}
}
Illustration (contd..)

What is the result?


 The resulting collection of empty bodies can
make the code harder to read and maintain.
To help you avoid implementing empty bodies, the API generally
includes an adapter class for each listener interface with
more than one method. For example, the MouseAdapter
class implements the MouseListener interface.
How to use an Adapter class?

/* Using an adapter class


*/
public class MyClass extends MouseAdapter {
....
someObject.addMouseListener(this);
....
public void mouseClicked(MouseEvent e) {
...//Event listener implementation goes
// here
}
}

Potrebbero piacerti anche