Event Handling Java Help

Event Handling

T’his cpapter examines an important aspect of Java that relates to’ applet events. As explained in Chapter 19, applets are event-driven programs. Thus, event handling is at the core of successful applet programming. Most events to which your appletwill respond are generated by the user. These events are passed to your applet in a variety of ways, with the specific method depending upon the actual event. The; are several types of events. The most commonly handled even are those generated by the mouse, .the keyboard, and various controls, such as a push button.    of Java’s event handling mechanism. It then . examines, sassed and interfaces, and develops that, demons tr fundamentals of event, processing; This chapter also explains how’ to use adapter classes, inner.classes, and anonymous inner classes to stem line event book make frequent use of these techniques.

Two Event handling Mechanisms

Before-beginning our discussion event handling, an important point must be made The.way in which events are handled by an applet changed significantly between the original version’ of Java (1.0) arid modern versions of Java, beginning with version  The LO method of event.handling is still supported, but it is not recommended for new programs. Also, many of the methods that support the old 1.0 event model have been ”.deprecated. The modem approach is the way that events should be handled by all new programs, Including those written for Java 2,.and thus is the method employed by. programs in this book.

The Delegation.Event Model

The modern approach to handling events is b event model, which. defines standard and consistent mechanisms-to-generate 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 wail’! until it receives an event.  received, the Roisterer . 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. ‘This provides an important benefit: notifications are sent only to listeners that want to receive them. ‘This is a more efficient way to handle events than the , design used by the old Java 1.0 approach. Previously, an event was propagated up the containment hierarchy until it was ‘handled by a component.  required components to·

Events 

In the delegation model, an  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  ‘ill a list, and clicking the mouse. Many ,other user operations could also be cited examples.  Events may also that are not directly caused by interactions  a user interface. For example, an event may be generated when  timer  exceeds a value, a software or hardware failure occurs, or is completed. You are free to define events that are appropriate for your application.

Event Sources

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 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:

Here, Type is the name of the event and el 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 form of such a method is this public void ere, Type is the name of the e-vent and el 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 keyboard and mouse  listeners.

JavaHelpOnline.com

  • Feel free to send us an inquiry, we will reply back in hours.
 

Verification

Posted on September 17, 2014 in Event Handling

Share the Story

About the Author

Back to Top