Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Academic Year 2024

Class: BIT28/BCS15

Chapter 03

JAVA Event Handling

Faculty of Computing
SIMAD University

JAVA Programming SIMAD University 1


Overview

Changing the state of an object is known as an event. For


example, click on button, dragging mouse etc. The java.awt.event
package provides many event classes and Listener interfaces for
event handling.
This section concentrates on the java.awt.event package. Which
has classes that are used for handling event caused by user
interaction
AWT GUI. Also you’ll learn, ActionEvents, ItemEvents, and
MouseEvents. And some other will take as assignment that will
see at the end of this chapter.
JAVA Programming SIMAD University 2
Events
An event is an object that describes a state change in a source.
events generated by interacting directly:
 pressing a button
 entering a character via the keyboard
 selecting an item in a list
 clicking the mouse
Also the events generated by not directly interacting
 when a timer expires
 a counter exceeds a value
 a hardware or software failure occurs
 an operation completed
JAVA Programming SIMAD University 3
Event Sources
 A source is an object that generates an event.
 Sources may generate more than one type of event.
 A source must register listeners in order for the listeners
to receive notifications about a specific type of event.
 Each type of event has its own registration method.

Multicasting is when an event occurs, all registered


listeners are notified and receive a copy of the event object.
Unicasting is some sources may allow only one listener to
register. only that registered listener is notified.
JAVA Programming SIMAD University 4
Interfaces
Event-handling requires the use of interface. The interfaces
looks like a class, except its method aren’t fully defined.
Each methods in an interface has a name, parameters list and
result type but no body. One common interface is named
ActionListener.

A class that implements ActionListener interface would


have to provide a method named actionPerformed with
one parameter of type ActionEvent.
Public void actionPerformed (ActionEvent e)

JAVA Programming SIMAD University 5


The keyword implements is used to tell the compiler that a
class will implement a particular interface.

The class may contain any number of variables constructors


and methods.
public class NewMain extends Frame implements ActionListener{
TextField txt;
Button b;

JAVA Programming SIMAD University 6


Handling Action Event
An action event occurs, whenever an action is performed by the
user. Examples: When the user clicks a button, chooses a
menu item, presses Enter in a text field.
The result is that an actionPerformed message is sent to all
action listeners that are registered on the relevant component.
To write an Action Listener, follow the steps given below :
 Declare an event handler class and specify that the class either
implements an ActionListener interface.
public class MyClass implements ActionListener {

JAVA Programming SIMAD University 7


 Register an instance of the event handler class as a listener on
one or more components. For example:
someComponent.addActionListener(instanceOfMyClass);

 Include code that implements the methods in listener


interface. For example:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action... }

JAVA Programming SIMAD University 8


JAVA Programming SIMAD University 9
JAVA Programming SIMAD University 10
Figure 3.1 Action Event

JAVA Programming SIMAD University 11


Handling Item Event
The Java ItemListener is notified whenever you click on the
checkbox. It is notified against ItemEvent. The ItemListener
interface is found in java.awt.event package. It has only
one method: itemStateChanged().

The itemStateChanged() method is invoked automatically


whenever you click or unclick on the registered checkbox
component.
public abstract void itemStateChanged(ItemEvent e)

JAVA Programming SIMAD University 12


JAVA Programming SIMAD University 13
JAVA Programming SIMAD University 14
Figure 3.2 Item Event
JAVA Programming SIMAD University 15
Handling Mouse Event

The Java MouseListener is notified whenever you change


the state of mouse. It is notified against MouseEvent.
The MouseListener interface is found in java.awt.event
package.
The four common methods.
 public abstract void mouseClicked(MouseEvent e);
 public abstract void mouseEntered(MouseEvent e);
 public abstract void mouseExited(MouseEvent e);
 public abstract void mousePressed(MouseEvent e);

JAVA Programming SIMAD University 16


JAVA Programming SIMAD University 17
JAVA Programming SIMAD University 18
Figure 3.3 Mouse Event

JAVA Programming SIMAD University 19


Chapter 03 Assignments

Home Work
Individual Home Work
Differentiate between the following FocusEvents, WindowEvents
and TextEvents (concept and practice)

JAVA Programming SIMAD University 20

You might also like