The KeyEvent Class
A KeyEvent is generated when keyboard input occurs. There are three types of key events, which are identified by these integer constants:KEY_PRESSED, KEY_RELEASED, and KEY_TYPED. The first two events are generated when any key is pressed or released. The last event occurs only when a character is generated. Remember, not all keypresses result in characters. For example, pressing shift does not generate a character.
There are many other integer constants that are defined by KeyEvent. For example, VK_0 through VK_9 and VK_A through VK_Z define the ASCII equivalents of the numbers and letters. Here are some others:
The VK constants specify virtual key codes and are independent of any modifiers, such as control, shift, or alt.
KeyEvent is a subclass of InputEvent. Here is one of its constructors: KeyEvent(Component src, int type, long when, int modifiers, int code, charch)
Here, src is a reference to the component that generated the event. The type of the event is specified by type. The system time at which the key was pressed is passed in when. The modifiers argument indicates which modifiers were pressed when this key event occurred. The virtual key code, such as VK_UP, VK_A, and so forth, is passed in code. The character equivalent (if one exists) is passed in ch. If no valid character exists, then chcontains
CHAR_UNDEFINED. For KEY_TYPED events, code will contain VK_UNDEFINED. The KeyEvent class defines several methods, but probably the most commonly used
ones are getKeyChar( ), which returns the character that was entered, and getKeyCode( ), which returns the key code. Their general forms are shown here:
char getKeyChar( ) int getKeyCode( )
If no valid character is available, then getKeyChar( ) returns CHAR_UNDEFINED. When a
KEY_TYPED event occurs, getKeyCode( ) returns VK_UNDEFINED.
The MouseEvent Class
There are eight types of mouse events. The MouseEvent class defines the following integer constants that can be used to identify them:
MOUSE_CLICKED : The user clicked the mouse.
MOUSE_DRAGGED : The user dragged the mouse.
MOUSE_ENTERED : The mouse entered a component.
MOUSE_EXITED : The mouse exited from a component.
MOUSE_MOVED : The mouse moved.
MOUSE_PRESSED : The mouse was pressed.
MOUSE_RELEASED : The mouse was released.
MOUSE_WHEEL : The mouse wheel was moved.
MouseEvent is a subclass of InputEvent. Here is one of its constructors:
MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup)
Here, src is a reference to the component that generated the event. The type of the event is specified by type. The system time at which the mouse event occurred is passed in when. The modifiers argument indicates which modifiers were pressed when a mouse event occurred.
The coordinates of the mouse are passed in x and y. The click count is passed in clicks. The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform.
Two commonly used methods in this class are getX( ) and getY( ). These return the X and Y coordinates of the mouse within the component when the event occurred. Their forms are shown here:
int getX( ) int getY( )
Alternatively, you can use the getPoint( ) method to obtain the coordinates of the mouse. It is shown here:
Point getPoint( )
It returns a Point object that contains the X,Y coordinates in its integer members: x and y.
The translatePoint( ) method changes the location of the event. Its form is shown here: void translatePoint(int x, int y)
Here, the arguments x and y are added to the coordinates of the event.
The getClickCount( ) method obtains the number of mouse clicks for this event. Its signature is shown here:
int getClickCount( )
The isPopupTrigger( ) method tests if this event causes a pop-up menu to appear on this platform. Its form is shown here:
boolean isPopupTrigger( )
Also available is the getButton( ) method, shown here: int getButton( )
It returns a value that represents the button that caused the event. For most cases, the return value will be one of these constants defined byMouseEvent:
The NOBUTTON value indicates that no button was pressed or released.
Also available are three methods that obtain the coordinates of the mouse relative to the screen rather than the component. They are shown here:
Point getLocationOnScreen( )
int getXOnScreen( )
The getLocationOnScreen( ) method returns a Point object that contains both the X and Y coordinate. The other two methods return the indicated coordinate.
The MouseWheelEvent Class
The MouseWheelEvent class encapsulates a mouse wheel event. It is a subclass of MouseEvent. Not all mice have wheels. If a mouse has a wheel, it is typically located between the left and right buttons. Mouse wheels are used for scrolling. MouseWheelEvent defines these two integer constants:
WHEEL_BLOCK_SCROLL : A page-up or page-down scroll event occurred.
WHEEL_UNIT_SCROLL : A line-up or line-down scroll event occurred.
Here is one of the constructors defined by MouseWheelEvent:
MouseWheelEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup,
int scrollHow, int amount, int count)
Here, src is a reference to the object that generated the event. The type of the event is specified by type. The system time at which the mouse event occurred is passed in when.
The modifiers argument indicates which modifiers were pressed when the event occurred. The coordinates of the mouse are passed in x and y. The number of clicks is passed in clicks. The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform. The scrollHowvalue must be either WHEEL_UNIT_SCROLL or WHEEL_BLOCK_ SCROLL. The number of units to scroll is passed in amount. The countparameter indicates the number of rotational units that the wheel moved.
MouseWheelEvent defines methods that give you access to the wheel event. To obtain the number of rotational units, call getWheelRotation( ), shown here:
int getWheelRotation( )
It returns the number of rotational units. If the value is positive, the wheel moved counterclockwise. If the value is negative, the wheel moved clockwise. JDK 7 added a method called getPreciseWheelRotation( ), which supports high-resolution wheels. It works like getWheelRotation( ), but returns a double.
To obtain the type of scroll, call getScrollType( ), shown next: int getScrollType( )
It returns either WHEEL_UNIT_SCROLL or WHEEL_BLOCK_SCROLL.
If the scroll type is WHEEL_UNIT_SCROLL, you can obtain the number of units to scroll by calling getScrollAmount( ). It is shown here:
int getScrollAmount( )
The TextEvent Class
Instances of this class describe text events. These are generated by text fields and text areas when characters are entered by a user or program.TextEvent defines the integer constant
The one constructor for this class is shown here: TextEvent(Object src, int type)
Here, src is a reference to the object that generated this event. The type of the event is specified by type.
The TextEvent object does not include the characters currently in the text component that generated the event. Instead, your program must use other methods associated with the text component to retrieve that information. This operation differs from other event objects discussed in this section. Think of a text event notification as a signal to a listener that it should retrieve information from a specific text component.
The WindowEvent Class
There are ten types of window events. The WindowEvent class defines integer constants that can be used to identify them. The constants and their meanings are shown here:
WINDOW_ACTIVATED : The window was activated.
WINDOW_CLOSED : The window has been closed.
WINDOW_CLOSING : The user requested that the window be closed.
WINDOW_DEACTIVATED : The window was deactivated.
WINDOW_DEICONIFIED : The window was deiconified.
WINDOW_GAINED_FOCUS : The window gained input focus.
WINDOW_ICONIFIED : The window was iconified.
WINDOW_LOST_FOCUS : The window lost input focus.
WINDOW_OPENED : The window was opened.
WINDOW_STATE_CHANGED : The state of the window changed.
WindowEvent is a subclass of ComponentEvent. It defines several constructors. The first is WindowEvent(Window src, int type)
Here, src is a reference to the object that generated this event. The type of the event is type. The next three constructors offer more detailed control:
WindowEvent(Window src, int type, Window other) WindowEvent(Window src, int type, int fromState, int toState) WindowEvent(Window src, inttype, Window other, int fromState, int toState)
Here, other specifies the opposite window when a focus or activation event occurs. The fromState specifies the prior state of the window, and toStatespecifies the new state that the window will have when a window state change occurs.
A commonly used method in this class is getWindow( ). It returns the Window object that generated the event. Its general form is shown here:
Window getWindow( )
WindowEvent also defines methods that return the opposite window (when a focus or activation event has occurred), the previous window state, and the current window state. These methods are shown here:
Window getOppositeWindow( )
int getOldState( )
int getNewState( )
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.