In the delegation model, an cu cut is an object that describes a stall' change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting and clicking the mouse. Many ,other user operations could also be examples. . Events may also that are not directly caused by interactions which a . For example, an event may be generated when timer exceeds a value, a software or hardware failure occurs, complete d.You are free to define events that are appropriate for your application.
A source is an object that 'generates an event. occurs when the internal state of that .. object changes in some way. Sources may. generate more than one type of even 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 Here, Type is the name of the event and c/ is a reference to the event listener. For example, the method that registers a keyboard event listener is called The method that-registers a mouse motion listener is called . When are event occurs, all registered listeners are notified and receive a copy of the event object. This is known as linguistically the event. In all cases, notifications arc sent only to listeners that register to receive them, . Some. sources may allow only one listener register. The general form of such a .method is this.
Here, Type is the name of the event and is a reference to the event listener. When such an event occurs, the registered listener is notified. This is known as uni casting the event A source must also provide a method that allows a listener to unregistered an interest in a specific type of event. The general form of such a method is this public void Here, Type is the name of the e-vent and is a reference to the event listener. For example, to remove a would call the methods that add or remove listeners are provided by the source that generates events. For example, the Component class provides methods to add and remove and mouse listeners .
A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources t6 receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications.The methods that receive and process events are defined in a set of interfaces found in interface defines two methods to receive notifications when the mouse is dragged or moved. Any object may receive and' process one or both of these events if it provides an implementation of this interface. Many other listener interfaces are discussed later in this and other chapters .
The classes that represent events are the core of Java's event handling mechanism. Thus, we begin our of event handling with a tour of the event classes. As you will see, they provide consistent, easy-to-use means of encapsulating events. At the root of the event class hierarchy is Event Object, which is in java.util. It is the super class for all events. Its one constructor is shown here: