Any program that uses a graphical user interface, such as a Java application written for Windows is event driven.
Events are supported by a number of packages, including java.util, java.awt and java.awt.event.
Most events to which your program will respond are generated when the user interacts whit a GUI based program. Types of events: those generated by the mouse, the keyboard, various GUI controls, such as a push button, scroll bar or check box.
The AWT (Abstract Window Toolkit) was Javas first GUI framework and it offers a simple way to present the basics of event handling.
The Delegation Event Model:
The modern approach to handling events is based on the delegation event model, which defines standard and consistent mechanisms to generate and process events. Its concept is quite simple: a source generates an event and sends it to one or more listeners. In this scheme, the listener simply waits until it receives an event. Once an event is received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events. A user interface element is able to „delegate“ the processing of an event to a separate piece of code.
In the delegation event model. Listeners must register with a source in order to receive an event notification, so notifications are sent only to listeners that want to receive them.
Events:
In the delegation model, an event is an object that describes a state change in a source. Among other causes, an event can be generated as a consequence of a person interacting with the elements in a graphical user interface (pressing a button, entering a character via the keyboard, selecting an item in a list and clicking the mouse).
Events can also occur that are not directly caused by interactions with a user interface. For example, an event can be generated when a timer expires, a counter exceeds a value, a software or hardware failure ocurs or an operation is completed.
Event Sources:
A source is an object that generates an event. This occur when the internal state of that object changes in some way. 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. Here is the general, form:
public void addTypeListener (TypeListener el)
In this example, Type is the name of the event and el is a reference to the event listener.
Examples:
-
addKeyListener() is a method that registers a keyboard event listener;
-
addMouseMotionListener() is a method that registers a mouse motion listener.
When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasating the event. In all cases, notifications are sent only to listeners that register to receive them.
Some sources may allow only one listener to register. This is known as unicasting the event and the general form of such a method is this:
public void addTypeListener (TypeListener el) throws
java.util.TooManyListenersException
In this exmple Type is also the name of the event and el is a reference to the event listener.
A source must also provide a method that allows a listener to unregister an interest in a specific type of event. The general form of such method is:
public void removeTypeListener (TypeListener el)
The methods that add or remove listeners are provided by the source that generates events. For example Component, which is a top-level class defined by the AWT, provides methods to add and remove keyboard and mouse event listeners.
Event Listeners:
A listener is an object that is notified when an evenr occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications. In other words, the listener must supply the event handlers.
The methods that receive and process events are defined in a set of interfaces, such as those found in java.awt.event.
Example:
The MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved. Any object may handle one or both of these events if it provides an implementation of this interface.
Important:
An event handler must return quickly. For the most part, a GUI program shgould not enter a “mode” of operation in which it maintains control for an extended period. Instead, it must perform specific actions in response to events and then return control to the run-time system. Failure to do this can cause your program to appear sluggish or even non-responsive. If your program needs to perform a repetitive task, such as scrolling a banner, it must do so by starting a separate thread. In short, when your program receives an event, it must process it immediately and then return.
That’s it for the first part. In part two we will talk about Event Classes.
Source: Herbert Schildt – Java. The Complete Reference. Tenth Edition – 2017.