Professional Documents
Culture Documents
JS Event Handling
JS Event Handling
G A N E S H PA I
A S S T. P R O F E S S O R G D I I I
D E PA RT M E N T O F C S E
N M A M I T, N I T T E
Textbook
Click to edit Master text styles
Second level
Third level
◦ Fourth level
◦ Fifth level
Unicasting event: Some sources may allow only one listener to register. The general form of method to register:
The same listener can listen to notifications from different objects. Many components to One Listener
Eve
Event Source1 nt o
A source provides a method to allow listener to unregister an event. bjec
t1
Component class provides methods to add and remove keyboard and mouse event listeners
Check box : Generates item events when the check box is selected or deselected.
List : Generates action events when an item is double-clicked; generates item events
when an item is selected or deselected.
Menu item : Generates action events when a menu item is selected; generates item
events when a checkable menu item is selected or deselected.
Scroll bar : Generates adjustment events when the scroll bar is manipulated.
Text components : Generates text events when the user enters a character.
The methods that receive and process events are defined in a set of interfaces
Ex.: 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 interface.
Event Classes
Adapter Classes
Inner Classes
int getValue( ) Returns amount of the adjustment. Returns the value represented by the
change
Constructor:
ComponentEvent(Component src, int type)
src: reference to the object that generated this event.
type: type of the event
Methods:
Component getComponent( ): returns the component that generated the event
boolean isTemporary( ) returns true if the change is temporary. Otherwise, it returns false
Constructors:
MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup)
src: reference to the component that generated the event.
type: type of the event
when: system time at which the mouse event occurred
modifiers: indicates which modifiers were pressed when a mouse event occurred.
x and y: The coordinates of the mous.
clicks: The click count
triggersPopup: flag indicates if this event causes a pop-up menu to appear on this platform.
Event Handling GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 32
8. MouseEvent Class
Methods:
int getX( ) Return the X coordinates of the mouse within the component when the event
occurred
int getY( ) Return the Y coordinates of the mouse within the component when the event
occurred
Point getPoint( ) Returns a Point object that contains the X,Y coordinates of the mouse, in its
integer members: x and y
void translatePoint(int x, int y) Changes the location of the event. Arguments x and y are added to the
coordinates of the event.
int getClickCount( ) Returns number of mouse clicks for this event
boolean isPopupTrigger( ) Tests if this event causes a pop-up menu to appear on this platform
int getButton( ) Returns a value representing the button that caused the event.
In most cases, value will be one of constants defined by MouseEvent:
NOBUTTON, BUTTON1, BUTTON2, BUTTON3
NOBUTTON value indicates that no button was pressed or released
Point getLocationOnScreen( )
int getXOnScreen( ) Returns coordinates of the mouse relative to the screen
int getYOnScreen( )
2 Integer Constants
WHEEL_BLOCK_SCROLL
WHEEL_UNIT_SCROLL
MouseWheelEvent (Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean
triggersPopup, int scrollHow, int amount, int count)
src: reference to the object that generated the event.
type: The type of the event.
when: The system time at which the mouse event occurred
modifiers: indicates which modifiers were pressed when the event occurred.
x and y: The coordinates of the mouse
clicks: The number of clicks the wheel has rotated.
triggersPopup: flag indicates if this event causes a pop-up menu to appear on this platform.
scrollHow: value must be either WHEEL_UNIT_SCROLL or WHEEL_BLOCK_SCROLL.
amount: The number of units to scroll is passed in.
count: the number of rotational units that the wheel moved.
Event Classes
Adapter Classes
Inner Classes
{
…
}
public void mouseEntered(MouseEvent me) { // Handle mouse entered. public void mouseMoved(MouseEvent me) { // Handle mouse moved
mouseX = 0; mouseY = 10; showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
msg = "Mouse entered."; }
repaint(); }
}
Event Handling GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 53
Using the Delegation Event Model
Handling Keyboard Events Program
KeyListener interface need to be implementing
When a key is pressed, a KEY_PRESSED event is generated resulting a call to the keyPressed( ) event
handler.
When the key is released, a KEY_RELEASED event is generated and keyReleased( ) handler is
executed.
If a character is generated by the keystroke, then a KEY_TYPED event is sent and the keyTyped( )
handler is invoked.
Each time the user presses a key, at least two and often three events are generated.
If actual characters are of interest, then key press and release events can be ignored.
If program needs to handle special keys, such as the arrow or function keys, it must watch for them
through the keyPressed( ) handler
The program echoes keystrokes to the applet window and shows the pressed/released status of each
key in the status window.