0% found this document useful (0 votes)
24 views33 pages

Ajp Unit 3

Uploaded by

adityakukade39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
24 views33 pages

Ajp Unit 3

Uploaded by

adityakukade39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 33
The a» \ _Complete Reference Contains comprehensive J Covers new features, Includes details on applets, coverage of the Java including assertions, NIO, | servlets, Swing, JavaBeans, language and libraries regular expressions, and the AWT, and collections | much more | } Z RTE @ scanned with OKEN Scanner = } Java™ 2: The Complete Reference \cdziven programs. Thus, event han explained in Chapter 19, applets are event-driven Progra handling Isat the core of successfal applet programming. Most events to which your applet will respond are generated by the user. These events are passed t0 Your applet in a vanety, of ways, with the specific method depending upon the actual event. There are several 'y8, with the specific method depending, uP snerated by the mouse, types of events, The most commonly handled events are those the keybourd, and various controls, such a8 @ push button, Events are supported by the favaawiavent pack “The chapter begins with an overview of Java’s event handling mechanism. It then, examines the main event classes and interfaces, and develops several examples that SCtnomstrate tne fundamentals of event processing. This chapter also explains how to se adapter classes inter siases, and anonymous inner classes to streamline event Ranaling code. The examples provided in the remainder of this book make frequent, tse of dese techniques. Ts chapter examines an important aspect of Java that relates to applets: events, Ag Before beginning our discussion of 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) and modern versions of Java, beginning with version 1.1 ‘The 1.0 method of event handling is still supported, but it is not recommencied for new programs. Also, many of the methods that support the old 1,0 event model have been Seprecated, The modern 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. Fre Delegation Event Model “The modern approach to handling events is based on the delegation vent model, whi defines standard and consistent mechanisms to generate and process event Is concept SSquite simple: a soure get snevent and sends it to one or more Istirs. inthis, ‘scheme, the listener simy its until it receives an event. Once received, the listener processes the event and then returns. The advantage ofthis ‘design is that the application IDgic dt processes events is cleanly sepatated from the user interface tonic ta gerrates thst events, A user interface element is able to “delegate” the presaseive steven S separate plece af code. ‘Tn the delagation event model, listeners must register with a source in order to receive tiaotifeation. This provides an important Genefit: Roubeationsene cent only (0 ier tht wnt receive tem Thin mov fet way to han events an he Hesign used by the old Java LO approsch. Previously. an event was hropepeted up the SoaEEument Klerarchy until twas handled by a covsponent Whe week ee eens © Scanned with OKEN Scanner foreceive events that they did not process eee el clinatee ic nt Process, and it wasted valuable time, The delegation Java also allows you to process events wit sora Ein be done by etter Sos tout using Bh deegtin cont model This tent. This techie fe discussed at the end of ‘Chapter 22. However, the delegation of Santer Aelegation coent models the preferred design for the reasons ‘The following sections define events and describe the roles of sources and listeners. Events Inthe delegation model, an evont is an object that deerbes a state change in a source ean be generated as a consequence Gf person interctng with the clement ira graphical user interface. Some of the activities that cause events to be gencrated are Frotsing a button, entering a character va the Keyboard, aclectng on teen ina at and Efcking the mouse: Many other user operations could also be eted ae examples. "Events may also occur that are not directly cused by interectons witha mee intrf0ce. For example, an event muy be generated whan a incr expires, a counter excoeds ‘elue, a eoftevare or hardware fare occurs, ran operation is completed. You ace fects define events that are appropriate for your application. Event Sources "A source is an object that generates an event, This occurs when the internal state of that object changes in soma way. Sources may generate more than one type of event. \cbsarce 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 add TypeListener(TypeListener el) ere. Tapes the name of the thevithed eae niters Ko tio at rgicaes a mone Then ewentcecur al ete Sb Th none a menting teers hat rege to cei oe inten to rogieter The general form of uch meth jatener el) Public void add TypeListener(TypeListener 6) throws java-util-TooManyListenersExcePt ion listener is called add MouseMotionListener( *ittencrs are notified and receive a copy of the event the event. In all cases, notifications are sent only to, © Scanned with OKEN Scanner G56 sava™ 2: The Complete Reference Here, Type is the name of the event and elisa reference to the event listener, When such an event occurs, the registered listener is notified. This is known as unicasting the event. TA source must slao provide # method that allows a listener to unzegister an interest ina specific type of event. The general form of such a method is this: Public void removeTypeListener(TypeListener el) Here, Type is the name of the event and el isa reference to the event listener. For example, toremove a keyboard listener, you would call removeKeyListener() “The methods that acid or remove listeners are provided by the source that generates, events. For example, the Component class provides methods to acd and remove Keyboard and mouse event listeners. Event Listeners ‘A listener is an object that is notified when an event occurs. Ithas two major requirements First, 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. "The methods that receive and process events are defined in a set of interfaces found in Jaawtevent, For example, the MouseMotionListener 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 interiace Many other listener interfaces are discusted later in this and other chapters. [_Jevent Classes © ‘The classes that represent events are at the core of Java’s event handling mechanism, ‘Thus, we begin our study of event handling with a tour of the event classes. As you. will see, they provide a consistent, easy-to-use means of encapsulating evento At the root of the Java event class hierarchy is EventObject, whichis in javacutl {tis the superclass for all events. Ite one constructor is shown here! EventObject(Object sr) ‘Here, se isthe object that generates this event. "EventObject contains two methods petSource() and toString(). The getSourcet) method returns the source ofthe event ts general orm ls sheen here ee ‘Object getSource() — As expected, toString() returns the string equivalent of the event ‘The class AWTEvent, defined within the java.awt package, is a subclass of EventObject. Itis the superclass (either directly or indirectly) of all AWT-based events © Scanned with OKEN Scanner -_ es7 Chapter 20: Event Wandling | | bys delegation event modal: getID() method canbe ued to determine the LRT SaaE The ignaturc or tine Reng mat can int getDO _sdaitional details about AWTEvent are provided at the end of Chapter 22. At this "itis important to know only that all of the other classes discussed in this sec#on PRiubelasses of AWTEvent ‘To summarize: fm EventObject isa superclass ofall events, fm AWTEvent isa superclass ofall AWT events thot are handled by the delegation “The package java.awt.event defines several types of events that are generated by ‘rious ser interface elements, Table 20-1 enumerates the most important of these event {chsses and provides a brief description of when they are generated. The most commonly lied constructors and methods in each clase are described in the following sections. Description lontvent Generated when a butt is pressed, ist tem i aon double-clicked, or a menu item is selected. AdjustmentEvent Generated when a scroll bar is manipulated, Componentivent Generated when a component is hidden, moved, resized, for becomes visible, ContainerEvent Generated when a component is added to or removed from a container, Focuskvent Generated when # component gains or loses Keyboard focus. Inputevent “Abstract super class for all component input event classes. temévent Generated when a check box or list item is clicked also Sani=S when a choice selection is made or a checkable SXSnu'item is selected or deselected, fai ert ie: © Scanned with OKEN Scanner Event Class UeeyEvent Generated when input fs received from the keyboard,

You might also like