`SFDC 1019
`
`
`
`
`
`
`
`
`
`
`
`In this chapter:
`(cid:127) Java 1.0 Event Model
`(cid:127) The Event Class
`(cid:127) The Java 1.1 Event
`Model
`
`4 E
`
`vents
`
`This chapter covers Java’s event-driven programming model. Unlike procedural
`programs, windows-based programs require an event-driven model in which the
`underlying environment tells your program when something happens. For exam-
`ple, when the user clicks on the mouse, the environment generates an event that it
`sends to the program. The program must then figure out what the mouse click
`means and act accordingly.
`
`This chapter covers two different event models, or ways of handling events. In Java
`1.0.2 and earlier, events were passed to all components that could possibly have an
`interest in them. Events themselves were encapsulated in a single Event class. Java
`1.1 implements a “delegation” model, in which events are distributed only to
`objects that have been registered to receive the event. While this is somewhat more
`complex, it is much more efficient and also more flexible, because it allows any
`object to receive the events generated by a component. In turn, this means that
`you can separate the user interface itself from the event-handling code.
`
`In the Java 1.1 event model, all event functionality is contained in a new package,
`java.awt.event. Within this package, subclasses of the abstract class AWTEvent rep-
`resent different kinds of events. The package also includes a number of Event-
`Listener inter faces that are implemented by classes that want to receive different
`kinds of events; they define the methods that are called when events of the appro-
`priate type occur. A number of adapter classes are also included; they correspond
`to the EventListener inter faces and provide null implementations of the methods
`in the corresponding listener. The adapter classes aren’t essential but provide a
`convenient shortcut for developers; rather than declaring that your class imple-
`ments a particular EventListener inter face, you can declare that your class
`extends the appropriate adapter.
`
`94
`
`10 July 2002 22:18
`
`SFDC 1019
`
`
`
`4.1
`
`JAV A 1.0 EVENT MODEL
`
`95
`
`The old and new event models are incompatible. Although Java 1.1 supports both,
`you should not use both models in the same program.
`
`4.1 Java 1.0 Event Model
`The event model used in versions 1.0 through 1.0.2 of Java is fairly simple. Upon
`receiving a user-initiated event, like a mouse click, the system generates an
`instance of the Event class and passes it along to the program. The program identi-
`fies the event’s target (i.e., the component in which the event occurred) and asks
`that component to handle the event. If the target can’t handle this event, an
`attempt is made to find a component that can, and the process repeats. That is all
`there is to it. Most of the work takes place behind the scenes; you don’t have to
`worr y about identifying potential targets or delivering events, except in a few spe-
`cial circumstances. Most Java programs only need to provide methods that deal
`with the specific events they care about.
`
`4.1.1 Identifying the Target
`All events occur within a Java Component. The program decides which component
`gets the event by starting at the outermost level and working in. In Figure 4-1,
`assume that the user clicks at the location (156, 70) within the enclosing Frame’s
`coordinate space. This action results in a call to the Frame’s deliverEvent()
`method, which determines which component within the frame should receive the
`event and calls that component’s deliverEvent() method. In this case, the process
`continues until it reaches the Button labeled Blood, which occupies the rectangu-
`lar space from (135, 60) to (181, 80). Blood doesn’t contain any internal compo-
`nents, so it must be the component for which the event is intended. Therefore, an
`action event is delivered to Blood, with its coordinates translated to fit within the
`button’s coordinate space—that is, the button receives an action event with the
`coordinates (21, 10). If the user clicked at the location (47, 96) within the Frame’s
`coordinate space, the Frame itself would be the target of the event because there is
`no other component at this location.
`
`To reach Blood, the event follows the component/container hierarchy shown in
`Figure 4-2.
`
`4.1.2 Dealing With Events
`Once deliverEvent() identifies a target, it calls that target’s handleEvent()
`method (in this case, the handleEvent() method of Blood) to deliver the event for
`processing. If Blood has not overridden handleEvent(), its default implementa-
`tion would call Blood’s action() method. If Blood has not overridden action(),
`its default implementation (which is inherited from Component) is executed and
`
`10 July 2002 22:18
`
`
`
`96
`
`CHAPTER 4: EVENTS
`
`Figure 4–1: deliverEvent
`
`Level 1
`
`Level 2
`
`Level 3
`
`deliverEvent
`
`DeliverEvent
`
`Panel 1
`
`of an
`
`Englishman
`
`deliverEvent
`
`Panel 2
`
`Panel 3
`
`deliverEvent
`
`Fe
`
`Fi
`
`Fo
`
`Fum
`
`I
`
`Smell
`
`The
`
`Blood
`
`Figure 4–2: deliverEvent screen model
`
`does nothing. For your program to respond to the event, you would have to pro-
`vide your own implementation of action() or handleEvent().
`
`handleEvent() plays a particularly important role in the overall scheme. It is really
`a dispatcher, which looks at the type of event and calls an appropriate method to
`do the actual work: action() for action events, mouseUp() for mouse up events,
`and so on. Table 4-1 shows the event-handler methods you would have to override
`when using the default handleEvent() implementation. If you create your own
`handleEvent(), either to handle an event without a default handler or to process
`events differently, it is best to leave these naming conventions in place. Whenever
`
`10 July 2002 22:18
`
`
`
`4.1
`
`JAV A 1.0 EVENT MODEL
`
`97
`
`you override an event-handler method, it is a good idea to call the overridden
`method to ensure that you don’t lose any functionality. All of the event handler
`methods return a boolean, which determines whether there is any further event
`processing; this is described in the next section, “Passing the Buck.”
`
`Table 4–1: Event Types and Event Handlers
`
`Event Type
`
`Event Handler
`
`MOUSE_ENTER
`
`mouseEnter()
`
`MOUSE_EXIT
`
`MOUSE_MOVE
`
`MOUSE_DRAG
`
`MOUSE_DOWN
`
`MOUSE_UP
`
`KEY_PRESS
`
`KEY_ACTION
`
`KEY_RELEASE
`
`mouseExit()
`
`mouseMove()
`
`mouseDrag()
`
`mouseDown()
`
`mouseUp()
`
`keyDown()
`
`keyDown()
`
`keyUp()
`
`KEY_ACTION_RELEASE keyUp()
`
`GOT_FOCUS
`
`LOST_FOCUS
`
`gotFocus()
`
`lostFocus()
`
`ACTION_EVENT
`
`action()
`
`4.1.3 Passing the Buck
`In actuality, deliverEvent() does not call handleEvent() directly. It calls the
`postEvent() method of the target component. In turn, postEvent() manages the
`calls to handleEvent(). postEvent() provides this additional level of indirection to
`monitor the return value of handleEvent(). If the event handler returns true, the
`handler has dealt with the event completely. All processing has been completed,
`and the system can move on to the next event. If the event handler returns false,
`the handler has not completely processed the event, and postEvent() will contact
`the component’s Container to finish processing the event. Using the screen in Fig-
`ure 4-1 as the basis, Example 4-1 traces the calls through deliverEvent(),
`postEvent(), and handleEvent(). The action starts when the user clicks on the
`Blood button at coordinates (156, 70). In short, Java dives into the depths of the
`screen’s component hierarchy to find the target of the event (by way of the
`method deliverEvent()). Once it locates the target, it tries to find something to
`deal with the event by working its way back out (by way of postEvent(), han-
`dleEvent(), and the convenience methods). As you can see, there’s a lot of
`
`10 July 2002 22:18
`
`
`
`98
`
`CHAPTER 4: EVENTS
`
`overhead, even in this relatively simple example. When we discuss the Java 1.1
`event model, you will see that it has much less overhead, primarily because it
`doesn’t need to go looking for a component to process each event.
`
`Example 4–1: The deliverEvent, postEvent, and handleEvent Methods
`
`DeliverEvent.deliverEvent (Event e) called
`DeliverEvent.locate (e.x, e.y)
`Finds Panel1
`Translate Event Coordinates for Panel1
`Panel1.deliverEvent (Event e)
`Panel1.locate (e.x, e.y)
`Finds Panel3
`Translate Event Coordinates for Panel3
`Panel3.deliverEvent (Event e)
`Panel3.locate (e.x, e.y)
`Finds Blood
`Translate Event Coordinates for Blood
`Blood.deliverEvent (Event e)
`Blood.postEvent (Event e)
`Blood.handleEvent (Event e)
`Blood.mouseDown (Event e, e.x, e.y)
`returns false
`return false
`Get parent Container Panel3
`Translate Event Coordinates for Panel3
`Panel3.postEvent (Event e)
`Panel3.handleEvent (Event e)
`Component.mouseDown (Event e, e.x, e.y)
`returns false
`return false
`Get parent Container Panel1
`Translate Event Coordinates for Panel1
`Panel1.postEvent (Event e)
`Panel1.handleEvent (Event e)
`Component.action (Event e, e.x, e.y)
`return false
`return false
`Get parent Container DeliverEvent
`Translate Event Coordinates for DeliverEvent
`DeliverEvent.postEvent (Event e)
`DeliverEvent.handleEvent
`DeliverEvent.action (Event e, e.x, e.y)
`return true
`return true
`return true
`return true
`return true
`return true
`return true
`return true
`return true
`return true
`
`10 July 2002 22:18
`
`
`
`4.1
`
`JAV A 1.0 EVENT MODEL
`
`99
`
`4.1.4 Overriding handleEvent()
`In many programs, you only need to override convenience methods like action()
`and mouseUp(); you usually don’t need to override handleEvent(), which is the
`high level event handler that calls the convenience methods. However, conve-
`nience methods don’t exist for all event types. To act upon an event that doesn’t
`have a convenience method (for example, LIST_SELECT), you need to override
`handleEvent() itself. Unfortunately, this presents a problem. Unlike the conve-
`nience methods, for which the default versions don’t take any action, han-
`dleEvent() does quite a lot: as we’ve seen, it’s the dispatcher that calls the
`convenience methods. Therefore, when you override handleEvent(), either you
`should reimplement all the features of the method you are overriding (a very bad
`idea), or you must make sure that the original handleEvent()is still executed to
`ensure that the remaining events get handled properly. The simplest way for you
`to do this is for your new handleEvent() method to act on any events that it is
`interested in and return true if it has handled those events completely. If the
`incoming event is not an event that your handleEvent() is interested in, you
`should call super.handleEvent() and return its return value. The following code
`shows how you might override handleEvent() to deal with a LIST_SELECT event:
`
`public boolean handleEvent (Event e) {
`// take care of LIST_SELECT
`if (e.id == Event.LIST_SELECT) {
`System.out.println ("Selected item: " + e.arg);
`return true;
`// LIST_SELECT handled completely; no further action
`} else {
`// make sure we call the overridden method to ensure
`// that other events are handled correctly
`return super.handleEvent (e);
`
`}
`
`}
`
`4.1.5 Basic Event Handlers
`The convenience event handlers like mouseDown(), keyUp(), and lostFocus() are
`all implemented by the Component class. The default versions of these methods do
`nothing and return false. Because these methods do nothing by default, when
`overriding them you do not have to ensure that the overridden method gets
`called. This simplifies the programming task, since your method only needs to
`return false if it has not completely processed the event. However, if you start to
`subclass nonstandard components (for example, if someone has created a fancy
`AudioButton, and you’re subclassing that, rather than the standard Button), you
`probably should explicitly call the overridden method. For example, if you are
`overriding mouseDown(), you should include a call to super.mouseDown(), just as
`we called super.handleEvent() in the previous example. This call is “good
`
`10 July 2002 22:18
`
`
`
`100
`
`CHAPTER 4: EVENTS
`
`housekeeping”; most of the time, your program will work without it. However, your
`program will break as soon as someone changes the behavior of the AudioButton
`and adds some feature to its mouseDown() method. Calling the super class’s event
`handler helps you write “bulletproof” code.
`
`The code below overrides the mouseDown() method. I’m assuming that we’re
`extending a standard component, rather than extending some custom compo-
`nent, and can therefore dispense with the call to super.mouseDown().
`
`public boolean mouseDown (Event e, int x, int y) {
`System.out.println (“Coordinates: “ + x + “-“ + y);
`if ((x > 100) || (y < 100))
`return false;
`// we’re not interested in this event; pass it on
`else
`// we’re interested;
`...
`// this is where event-specific processing goes
`return true;
`// no further event processing
`
`}
`
`Here’s a debugging hint: when overriding an event handler, make sure that the
`parameter types are correct—remember that each convenience method has differ-
`ent parameters. If your overriding method has parameters that don’t match the
`original method, the program will still compile correctly. However, it won’t work.
`Because the parameters don’t match, your new method simply overloads the origi-
`nal, rather than overriding it. As a result, your method will never be called.
`
`4.2 The Event Class
`An instance of the Event class is a platform-independent representation that
`encapsulates the specifics of an event that happens within the Java 1.0 model. It
`contains everything you need to know about an event: who, what, when, where,
`and why the event happened. Note that the Event class is not used in the Java 1.1
`event model; instead, Java 1.1 has an AWTEvent class, with subclasses for different
`event types.
`
`When an event occurs, you decide whether or not to process the event. If you
`decide against reacting, the event passes through your program quickly without
`anything happening. If you decide to handle the event, you must deal with it
`quickly so the system can process the next event. If handling the event requires a
`lot of work, you should move the event-handling code into its own thread. That
`way, the system can process the next event while you go off and process the first. If
`you do not multithread your event processing, the system becomes slow and unre-
`sponsive and could lose events. A slow and unresponsive program frustrates users
`and may convince them to find another solution for their problems.
`
`10 July 2002 22:18
`
`
`
`4.2 THE EVENT CLASS
`
`101
`
`4.2.1 Variables
`Event contains ten instance variables that offer all the specific information for a
`particular event.
`
`Instance variables
`public Object arg
`The arg field contains some data regarding the event, to be interpreted by the
`recipient. For example, if the user presses Return within a TextField, an
`Event with an id of ACTION_EVENT is generated with the TextField as the
`target and the string within it as the arg. See a description of each specific
`event to find out what its arg means.
`public int clickCount
`The clickCount field allows you to check for double clicking of the mouse.
`This field is relevant only for MOUSE_DOWN events. There is no way to specify the
`time delta used to determine how quick a double-click needs to be, nor is
`there a maximum value for clickCount. If a user quickly clicks the mouse
`four times, clickCount is four. Only the passage of a system-specific time delta
`will reset the value so that the next MOUSE_DOWN is the first click. The incre-
`menting of clickCount does not care which mouse button is pressed.
`public Event evt
`The evt field does not appear to be used anywhere but is available if you wish
`to pass around a linked list of events. Then your program can handle this
`event and tell the system to deal with the next one (as demonstrated in the fol-
`lowing code), or you can process the entire chain yourself.
`
`public boolean mouseDown (Event e, int x, int y) {
`System.out.println ("Coordinates: " + x + "-" + y);
`if (e.evt != null)
`postEvent (e.evt);
`return true;
`
`}
`
`public int id
`The id field of Event contains the identifier of the event. The system-gener-
`ated events are the following Event constants:
`
`WINDOW_DESTROY
`WINDOW_EXPOSE
`WINDOW_ICONIFY
`WINDOW_DEICONIFY
`
`MOUSE_ENTER
`MOUSE_EXIT
`MOUSE_DRAG
`SCROLL_LINE_UP
`
`10 July 2002 22:18
`
`
`
`102
`
`CHAPTER 4: EVENTS
`
`SCROLL_LINE_DOWN
`KEY_PRESS
`SCROLL_PAGE_UP
`KEY_RELEASE
`SCROLL_PAGE_DOWN
`KEY_ACTION
`KEY_ACTION_RELEASE SCROLL_ABSOLUTE
`MOUSE_DOWN
`LIST_SELECT
`MOUSE_UP
`LIST_DESELECT
`MOUSE_MOVE
`ACTION_EVENT
`
`As a user, you can create your own event types and store your own unique
`event ID here. In Java 1.0, there is no formal way to prevent conflicts between
`your events and system events, but using a negative IO is a good ad-hoc
`method. It is up to you to check all the user events generated in your program
`in order to avoid conflicts among user events.
`public int key
`For keyboard-related events, the key field contains the integer representation
`of the keyboard element that caused the event. Constants are available for the
`keypad keys. To examine key as a character, just cast it to a char. For nonkey-
`board-related events, the value is zero.
`pubic int modifiers
`The modifiers field shows the state of the modifier keys when the event hap-
`pened. A flag is set for each modifier key pressed by the user when the event
`happened. Modifier keys are Shift, Control, Alt, and Meta. Since the middle
`and right mouse key are indicated in a Java event by a modifier key, one rea-
`son to use the modifiers field is to determine which mouse button triggered
`an event. See Section 4.2.4 for an example.
`public Object target
`The target field contains a reference to the object that is the cause of the
`event. For example, if the user selects a button, the button is the target of the
`event. If the user moves the mouse into a Frame, the Frame is the target. The
`target indicates where the event happened, not the component that is deal-
`ing with it.
`public long when
`The when field contains the time of the event in milliseconds. The following
`code converts this long value to a Date to examine its contents:
`
`Date d = new Date (e.when);
`
`public int x
`public int y
`The x and y fields show the coordinates where the event happened. The coor-
`dinates are always relative to the top left corner of the target of the event and
`get translated based on the top left corner of the container as the event gets
`
`10 July 2002 22:18
`
`
`
`4.2 THE EVENT CLASS
`
`103
`
`passed through the containing components. (See the previous Section 4.1.1
`for an example of this translation.) It is possible for either or both of these to
`be outside the coordinate space of the applet (e.g., if user quickly moves the
`mouse outside the applet).
`
`4.2.2 Constants
`Numerous constants are provided with the Event class. Several designate which
`event happened (the why). Others are available to help in determining the func-
`tion key a user pressed (the what). And yet more are available to make your life
`easier.
`
`When the system generates an event, it calls a handler method for it. To deal with
`the event, you have to override the appropriate method. The different event type
`sections describe which methods you override.
`
`Key constants
`These constants are set when a user presses a key. Most of them correspond to
`function and keypad keys; since such keys are generally used to invoke an action
`from the program or the system, Java calls them action keys and causes them to gen-
`erate a different Event type (KEY_ACTION) from regular alphanumeric keys
`(KEY_PRESS).
`
`Table 4-2 shows the constants used to represent keys and the event type that uses
`each constant. The values, which are all declared public static final int,
`appear in the key variable of the event instance. A few keys represent ASCII char-
`acters that have string equivalents such as \n. Black stars (#) mark the constants
`that are new in Java 1.1; they can be used with the 1.0 event model, provided that
`you are running Java 1.1. Java 1.1 events use a different set of key constants
`defined in the KeyEvent class.
`
`Table 4–2: Constants for Keys in Java 1.0
`
`Constant
`HOME
`
`Event Type
`KEY_ACTION
`
`END
`
`PGUP
`
`PGDN
`
`UP
`
`DOWN
`
`LEFT
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`RIGHT
`
`KEY_ACTION
`
`F1
`
`KEY_ACTION
`
`Constant
`F9
`
`F10
`
`F11
`
`F12
`PRINT_SCREEN#
`SCROLL_LOCK#
`CAPS_LOCK#
`NUM_LOCK#
`PAUSE#
`
`Event Type
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`10 July 2002 22:18
`
`
`
`104
`
`CHAPTER 4: EVENTS
`
`Table 4–2: Constants for Keys in Java 1.0 (continued)
`
`Constant
`F2
`
`Event Type
`KEY_ACTION
`
`F3
`
`F4
`
`F5
`
`F6
`
`F7
`
`F8
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`KEY_ACTION
`
`Event Type
`KEY_ACTION
`
`Constant
`INSERT#
`ENTER (\n)#
`KEY_PRESS
`BACK_SPACE (\b)# KEY_PRESS
`TAB (\t)#
`KEY_PRESS
`ESCAPE#
`DELETE#
`
`KEY_PRESS
`
`KEY_PRESS
`
`Modifiers
`Modifiers are keys like Shift, Control, Alt, or Meta. When a user presses any key or
`mouse button that generates an Event, the modifiers field of the Event instance is
`set. You can check whether any modifier key was pressed by ANDing its constant
`with the modifiers field. If multiple modifier keys were down at the time the event
`occurred, the constants for the different modifiers are ORed together in the field.
`
`public static final int ALT_MASK
`public static final int CTRL_MASK
`public static final int META_MASK
`public static final int SHIFT_MASK
`
`When reporting a mouse event, the system automatically sets the modifiers field.
`Since Java is advertised as supporting the single-button mouse model, all buttons
`generate the same mouse events, and the system uses the modifiers field to differ-
`entiate between mouse buttons. That way, a user with a one- or two-button mouse
`can simulate a three-button mouse by clicking on his mouse while holding down a
`modifier key. Table 4-3 lists the mouse modifier keys; an applet in Section 4.2.4
`demonstrates how to differentiate between mouse buttons.
`
`Table 4–3: Mouse Button Modifier Keys
`
`Mouse Button
`
`Modifier Key
`
`Left mouse button
`Middle mouse button
`Right mouse button
`
`None
`ALT_MASK
`
`META_MASK
`
`10 July 2002 22:18
`
`
`
`4.2 THE EVENT CLASS
`
`105
`
`For example, if you have a three-button mouse, and click the right button, Java
`generates some kind of mouse event with the META_MASK set in the modifiers field.
`If you have a one-button mouse, you can generate the same event by clicking the
`mouse while depressing the Meta key.
`
`NOTE
`
`If you have a multibutton mouse and do an Alt+right mouse or
`Meta+left mouse, the results are platform specific. You should get a
`mouse event with two masks set.
`
`Key events
`The component peers deliver separate key events when a user presses and releases
`nearly any key. KEY_ACTION and KEY_ACTION_RELEASE are for the function and
`arrow keys, while KEY_PRESS and KEY_RELEASE are for the remaining control and
`alphanumeric keys.
`
`public static final int KEY_ACTION
`The peers deliver the KEY_ACTION event when the user presses a function
`or keypad key. The default Component.handleEvent() method calls the
`keyDown() method for this event. If the user holds down the key, this event is
`generated multiple times. If you are using the 1.1 event model, the interface
`method KeyListener.keyPressed() handles this event.
`public static final int KEY_ACTION_RELEASE
`The peers deliver the KEY_ACTION_RELEASE event when the user releases a
`function or keypad key. The default handleEvent() method for Component
`calls the keyUp() method for this event. If you are using the 1.1 event model,
`the KeyListener.keyReleased() inter face method handles this event.
`public static final int KEY_PRESS
`The peers deliver the KEY_PRESS event when the user presses an ordinary key.
`The default Component.handleEvent() method calls the keyDown() method
`for this event. Holding down the key causes multiple KEY_PRESS events to be
`generated. If you are using the 1.1 event model, the interface method KeyLis-
`tener.keyPressed() handles this event.
`public static final int KEY_RELEASE
`The peers deliver KEY_RELEASE events when the user releases an ordinary key.
`The default handleEvent() method for Component calls the keyUp() method
`for this event. If you are using the 1.1 event model, the interface method
`KeyListener.keyReleased() handles this event.
`
`10 July 2002 22:18
`
`
`
`106
`
`NOTE
`
`NOTE
`
`CHAPTER 4: EVENTS
`
`If you want to capture arrow and keypad keys under the X Window
`System, make sure the key codes are set up properly, using the
`xmodmap command.
`
`Some platforms generate events for the modifier keys by themselves,
`whereas other platforms require modifier keys to be pressed with
`another key. For example, on a Windows 95 platform, if Ctrl+A is
`pressed, you would expect one KEY_PRESS and one KEY_RELEASE.
`However, there is a second KEY_RELEASE for the Control key. Under
`Motif, you get only a single KEY_RELEASE.
`
`Window events
`Window events happen only for components that are children of Window. Several
`of these events are available only on certain platforms. Like other event types, the
`id variable holds the value of the specific event instance.
`
`public static final int WINDOW_DESTROY
`The peers deliver the WINDOW_DESTROY event whenever the system tells a win-
`dow to destroy itself. This is usually done when the user selects the window
`manager’s Close or Quit window menu option. By default, Frame instances do
`not deal with this event, and you must remember to catch it yourself. If you
`are using the 1.1 event model, the WindowListener.windowClosing() inter-
`face method handles this event.
`public static final int WINDOW_EXPOSE
`The peers deliver the WINDOW_EXPOSE event whenever all or part of a window
`becomes visible. To find out what part of the window has become uncovered,
`use the getClipRect() method (or getClipBounds() in Java version 1.1) of
`the Graphics parameter to the paint() method. If you are using the 1.1 event
`model, the WindowListener.windowOpening() inter face method most closely
`corresponds to the handling of this event.
`public static final int WINDOW_ICONIFY
`The peers deliver the WINDOW_ICONIFY event when the user iconifies the win-
`dow. If you are using the 1.1 event model, the interface method WindowLis-
`tener.windowIconified() handles this event.
`public static final int WINDOW_DEICONIFY
`The peers deliver the WINDOW_DEICONIFY event when the user de-iconifies the
`window. If you are using the 1.1 event model, the interface method Win-
`dowListener.windowDeiconified() handles this event.
`
`10 July 2002 22:18
`
`
`
`4.2 THE EVENT CLASS
`
`107
`
`public static final int WINDOW_MOVED
`The WINDOW_MOVED event signifies that the user has moved the window. If you
`are using the 1.1 event model, the ComponentListener.componentMoved()
`inter face method handles this event.
`
`Mouse events
`The component peers deliver mouse events when a user presses or releases a
`mouse button. Events are also delivered whenever the mouse moves. In order to
`be platform independent, Java pretends that all mice have a single button. If you
`press the second or third button, Java generates a regular mouse event but sets the
`event’s modifers field with a flag that indicates which button was pressed. If you
`press the left button, no modifiers flags are set. Pressing the center button sets the
`ALT_MASK flag; pressing the right button sets the META_MASK flag. Therefore, you
`can determine which mouse button was pressed by looking at the Event.modi-
`fiers attribute. Furthermore, users with a one-button or two-button mouse can
`generate the same events by pressing a mouse button while holding down the Alt
`or Meta keys.
`
`NOTE
`
`Early releases of Java (1.0.2 and earlier) only propagated mouse
`events from Canvas and Container objects. With the 1.1 event
`model, the events that different components process are better
`defined.
`
`public static final int MOUSE_DOWN
`The peers deliver the MOUSE_DOWN event when the user presses any mouse but-
`ton. This action must occur over a component that passes along the
`MOUSE_DOWN event. The default Component.handleEvent() method calls the
`mouseDown() method for this event. If you are using the 1.1 event model, the
`MouseListener.mousePressed() inter face method handles this event.
`public static final int MOUSE_UP
`The peers deliver the MOUSE_UP event when the user releases the mouse but-
`ton. This action must occur over a component that passes along the MOUSE_UP
`event. The default handleEvent() method for Component calls the mouseUp()
`method for this event. If you are using the 1.1 event model, the interface
`method MouseListener.mouseReleased() handles this event.
`public static final int MOUSE_MOVE
`The peers deliver the MOUSE_MOVE event whenever the user moves the mouse
`over any part of the applet. This can happen many, many times more than you
`want to track, so make sure you really want to do something with this event
`before trying to capture it. (You can also capture MOUSE_MOVE events and
`
`10 July 2002 22:18
`
`
`
`108
`
`CHAPTER 4: EVENTS
`
`without losing much, choose to deal with only every third or fourth move-
`ment.) The default handleEvent() method calls the mouseMove() method for
`the event. If you are using the 1.1 event model, the interface method MouseMo-
`tionListener.mouseMoved() handles this event.
`public static final int MOUSE_DRAG
`The peers deliver the MOUSE_DRAG event whenever the user moves the mouse
`over any part of the applet with a mouse button depressed. The default
`method handleEvent() calls the mouseDrag() method for the event. If you are
`using
`the 1.1 event model,
`the
`interface method MouseMotionLis-
`tener.mouseDragged() handles this event.
`public static final int MOUSE_ENTER
`The peers deliver the MOUSE_ENTER event whenever the cursor enters a compo-
`nent. The default handleEvent() method calls the mouseEnter() method for
`the event. If you are using the 1.1 event model, the interface method
`MouseListener.mouseEntered() handles this event.
`public static final int MOUSE_EXIT
`The peers deliver the MOUSE_EXIT event whenever the cursor leaves a compo-
`nent. The default handleEvent() method calls the mouseExit() method for
`the event. If you are using the 1.1 event model, the interface method
`MouseListener.mouseExited() handles this event.
`
`Scrolling events
`The peers deliver scrolling events for the Scrollbar component. The objects that
`have a built-in scrollbar (like List, ScrollPane, and TextArea) do not generate
`these events. No default methods are called for any of the scrolling events. They
`must be dealt with in the handleEvent() method of the Container or a subclass of
`the Scrollbar. You can determine which particular event occurred by checking
`the id variable of the event, and find out the new position of the thumb by looking
`at the arg variable or calling getValue() on the scrollbar. See also the description
`of the AdjustmentListener inter face later in this chapter.
`
`public static final int SCROLL_LINE_UP
`The scrollbar peers deliver the SCROLL_LINE_UP event when the user presses
`the arrow pointing up for the vertical scrollbar or the arrow pointing left for
`the horizontal scrollbar. This decreases the scrollbar setting by one back
`toward the minimum value. If you are using the 1.1 event model, the interface
`method AdjustmentListener.adjustmentValueChanged() handles this event.
`
`10 July 2002 22:18
`
`
`
`4.2 THE EVENT CLASS
`
`109
`
`public static final int SCROLL_LINE_DOWN
`The peers deliver the SCROLL_LINE_DOWN event when the user presses the
`arrow pointing down for the vertical scrollbar or the arrow pointing right for
`the horizontal scrollbar. This increases the scrollbar setting by one toward the
`maximum value. If you are using the 1.1 event model, the interface method
`AdjustmentListener.adjustmentValueChanged() handles this event.
`public static final int SCROLL_PAGE_UP
`The peers deliver the SCROLL_PAGE_UP event when the user presses the mouse
`with the cursor in the area between the slider and the decrease arrow. This
`decreases the scrollbar setting by the paging increment, which defaults to 10,
`back toward the minimum value. If you are using the 1.1 event model, the
`inter face method AdjustmentListener.adjustmentValueChanged() handles
`this event.
`public static final int SCROLL_PAGE_DOWN
`The peers deliver the SCROLL_PAGE_DOWN event when the user presses the
`mouse with the cursor in the area between the slider and the increase arrow.
`This increases the scrollbar setting by the paging increment, which defaults to
`10, toward the maximum value. If you are using the 1.1 event model, the inter-
`face method AdjustmentListener.adjustmentValueChanged() handles this
`event.
`public static final int SCROLL_ABSOLUTE
`The peers deliver the SCROLL_ABSOLUTE event when the user drags the slider
`part of the scrollbar. There is no set time period or distance between multiple
`SCROLL_ABSOLUTE events. If you are using the Java version 1.1 event model, the
`AdjustmentListener.adjustmentValueChanged() inter face method handles
`this event.
`public sta