Event
classes are the classes that represent events at the core of java’s event
handling mechanism.
At
the root of the Java event class hierarchy is EventObject, which is in java.util.
It is the superclass of all events. Its constructor is:
EventObject
(Object src)
Here,
src is the objects that generates the event.
EventObject
contains two objects: getSource() and
toString().
The
getSource() method returns the
source of the event. Its general form is:
Object
getSource()
toString()
method returns the string equivalent of the event.
The
class AWTEvent, defined within the java.awt package, is a subclass of EventObject. It the superclass of all
AWT-based events used by delegation event model.
The
main classes in java.awt.event:
The
ActionEvent Class:
The
ActionEvent class defines four integer constants that can be used to identify
any modifiers associated with an action event: ALT_MASK, CTRL_MASK, META_MASK, and SHIFT_MASK. There
is integer constant ACTION_PERFORMED which
can be used to identify action events.
ActionEvent
has three constructors:
ActionEvent
(Object src, int type, String cmd)
ActionEvent
(Object src, int type, String cmd, int modifiers)
ActionEvent
(Object src, int type, String cmd, long when, int modifiers)
Here, src is a
reference to the object that generated this event. The type of the event is
specified by type and its command string is cmd. The argument modifiers
indicate which modifier keys (ALT, CTRL, META, and/or SHIFT) were pressed when
the event was generated. The parameter specifies when the event occurred.
Command
name for the invoking ActionEvent object
by using the getActionCommand() method:
String
getActionCommand()
For example,
when a button is pressed, an action event is generated that has a command name
equal to the label on that button.
The
getModifier() method returns a value
that indicates which modifier keys were pressed when the event was generated.
int
getModifier()
The
getWhen() method returns the time at
which event took palce.
long getWhen()
The
AdjustmentEvent Class:
There
are five types of AdjustmentEvent. The AdjustmentEvent class defines integer
constants that can be used to identify them.
BLOCK_DECREMENT
|
The
user clicked inside the scroll bar to decrease its value.
|
BLOCK_INCREMENT
|
The
user clicked inside the scroll bar to increase its value.
|
TRACK
|
The
slider was dragged.
|
UINT_DECREMENT
|
The
button at the end of scroll bar was clicked to decrease its value.
|
UNIT_INCREMENT
|
The
button at the end of scroll bar was clicked to increase its value.
|
In addition
there is an integer constant, ADJUSTMENT_VALUE_CHANGED that indicates that a
change has occurred.
AdjustmentEvent
has a constructors:
AdjustmentEvent (Adjustable
src, int id, int type, int data)
Here, src is a
reference to the object that generated this event. The type of the adjustment
is specified by type and its associated data is data.
The
getAdjustable() method returns
object that generated the event.
Adjustable get
Adjustable()
For example,
when a button is pressed, an action event is generated that has a command name
equal to the label on that button.
The
getAdjustmentType() method returns the
type of adjustment event.
int getAdjustmentType()
The
getValue() method returns the amount
of adjustment.
long getValue()
The ComponentEvent
Class:
There
are four types of AdjustmentEvent. The AdjustmentEvent class defines integer
constants that can be used to identify them.
COMPONENT_HIDDEN
|
The
component was hidden.
|
COMPONENT_MOVED
|
The
component was moved.
|
COMPONENT_RESIZED
|
The
component was resized.
|
COMPONENT_SHOWN
|
The
component became visible.
|
ComponentEvent
has a constructors:
ComponentEvent (Component
src, int type)
Here, src is a
reference to the object that generated this event. The type of the adjustment
is specified by type.
The
ComponentEvent is the superclass either directly or indirectly of
ContainerEvent, FocusEvent, KeyEvent, MouseEvent, and WindowEvent.
The
getComponent() method returns the component
that generated the event.
Component getComponent
()
The
ContainerEvent Class:
There
are two types of container events. The ContainerEvent class defines int
constants that can be used to identify them: COMPONENT_ADDED and COMPONENT_REMOVED.
They indicate that a component has been added to or removed from the container.
ContainerEvent
is a subclass of ComponentEvent and has a constructor:
ContainerEvent
(Component src, int type, Component comp)
Here, src is a
reference to the object that generated this event. The type of the event is
specified by type and the component that has been added to or removed from the
container class.
The
getContainer() method returns a reference
to the container that generated the event.
Container getContainer
()
The
getChild() method returns a
reference to the component that was added to or removed from the container.
Component getChild()
The FocusEvent
Class:
FocusEvents
are identified by integer constants FOCUS_GAINED
and FOCUS_LOST.
FocusEvent
is a subclass of ComponentEvent and has these constructor:
FocusEvent
(Component src, int type)
FocusEvent
(Component src, int type, boolean temporaryFlag)
FocusEvent
(Component src, int type, boolean temporaryFlag, Component other)
Here, src is a
reference to the object that generated this event. The type of the event is
specified by type. The argument temporaryFlag is set to true if the focus event
is temporary. Otherwise it is set to false. The other component involved in the
focus change, called the opposite component, is passed in other. Therefore, if
a FOCUS_GAINED event occurred, other will refer to the component that lost
focus.
The
getOppositeComponent() method used
to determine the other component.
Component getOppositeComponent
()
The
isTemporary() method indicates that
if this focus change is temporary.
boolean isTemporary()
The method
returns true if the change is temporary.
The InputEvent
Class:
The
abstract class InputEvents is a subclass of ComponentEvent and is a superclass
for component input events. Its subclasses are KeyEvent and MouseEvent.
InputEvent
defines several integer constants that represent any modifiers, such as the
control key being pressed, that might be associated with the event. The
following eight modifiers are defined:
ALT_MASK
|
BUTTON2_MASK
|
META_MASK
|
ALT_GRAPH_MASK
|
BUTTON3_MASK
|
SHIFT_MASK
|
BUTTON1_MASK
|
CTRL_MASK
|
|
However,
because of possible conflict between the modifiers used by keyboard events and
mouse events, and other issues, the following extended modifier values were
addes:
ALT_DOWN_MASK
|
BUTTON2_DOWN
_MASK
|
META_DOWN
_MASK
|
ALT_GRAPH_DOWN
_MASK
|
BUTTON3_DOWN
_MASK
|
SHIFT_DOWN
_MASK
|
BUTTON1_DOWN
_MASK
|
CTRL_DOWN
_MASK
|
|
To
test if a modifier was pressed at the time an event is generated, use the isAltDown(), isAltGraphDown(), isControlDown(),
isMetaDown(), and isShiftDown() method. The forms of
these methods are shown below:
boolean
isAltDown()
boolean
isAltGraphDown()
boolean
isControlDown()
boolean
isMetaDown()
boolean
isShiftDown()
The getModifier() method is used to obtain
a value that contains all of the original modifier flags.
int
getModifiers()
The getModifierEx() method is used to
obtain the extended modifiers.
int
getModifiersEx()
The ItemEvent
Class:
There
are two types of item events which are identified by two integer constants:
DESELECTED
|
The
user deselected an item.
|
SELECTED
|
The
user selected an item.
|
ItemEvent
defiens one integer constant, ITEM_STATE_CHANGED that signifies a change of
state.
ItemEvent
uses the constructor:
ItemEvent (ItemSelectable
src, int type, Object entry, int state)
Here, src is a
reference to the object that generated this event. The type of the event is
specified by type. The specific item that generated the item event is passed in
entry. The current state of that item is in state.
The
getItem() method returns a reference
to the item that generated an event.
Object getItem
()
The
getItemSelectable() method returns a
reference to the ItemSelectable object that generated an event.
ItemSelectable
getItemSelectable ()
The
getStateChange() method returns the
state change(i.e, SELECTED or DESELECTED) for an event:
int
getStateChange ()
The KeyEvent
Class:
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 a key is pressed or released. The last
event occurs when a character is generated.
Many
other integer constants that are defined by KeyEvent:
VK_ALT
|
VK_DOWN
|
VK_LEFT
|
VK_RIGHT
|
VK_CANCLE
|
VK_ENTER
|
VK_
PAGE_DOWN
|
VK_SHIFT
|
VK_CONTROL
|
VK_ESCAPE
|
VK_PAGE_UP
|
VK_UP
|
The VK constants specifies virtual key
codes.
KeyEvent
is a subclass of InputEvent and uses the constructor:
KeyEvent (Component
src, int type, long when, int modifiers, int code, char ch)
Here, src is a
reference to the object that generated this event. The type of the event is
specified by type. The system time at which the key is pressed is passed in when.
The modifier argument specifies which modifiers were pressed when this key
event occurred. The virtual key code is passed in code. The character
equivalent is passed in ch. If no valid character exist, then ch contains
CHAR_UNDEFINED. For KEY_TYPED events, code will contain VK_UNDEFINED.
The
getKeyChar() method returns the
character that was entered and getKeyCode()
method returns the key code.
char getKeyChar
()
int getKeyCode
()
The MouseEvent
Class:
There
are eigth types of mouse events. MouseEvent class defines the following integer
constants:
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
class is a subclass of InputEvent. It uses the constructor:
MouseEvent (Component
src, int type, long when, int modifier, int x,int y, int clicks, boolean
triggersPopup)
Here, src is a
reference to the object that generated this event. The type of the event is
specified by type. The system time at which the mouse event occurred is passed
in when. The modifier argument specifies which modifiers were pressed when this
mouse event occurred. The coordinates of the mouse are passed in x and y. The
click count is passed in clicks.The triggerPopup flag indicates if this event
causes a popup menu to appear on this platform.
The
getX() and getY() methods returns the x and y coordinates of the mouse within
the component when the event occurred.
int getX()
int getY()
Another method
getPoint() method is used to obtain
the coordinates of the mouse:
Point
getPoint()
The
translatePoint() method changes the
location of the event.
void
translatePoint (int x, int y)
Here, the
arguments x and y are added to the coordinates of the event.
The
getClickCount() method returns the number
of mouse clicks for an event:
int getClickCount
()
The
isPopupTrigger() method tests if
this event causes a pop-up menu to appear on this platform.
boolean
isPopupTrigger()
The
getButton() method that represent
the button that caused the event:
int
getButton()
NOBUTTON
|
BUTTON1
|
BUTTON2
|
BUTTON3
|
NOBUTTON
indicates that no button was pressed or released.
The
MouseWheelEvent Class:
MouseWheelEvent
is a subclass of MouseEvent. Mouse wheels are used for scrolling.
MouseWheelEvent defines 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.
|
MouseWheelEvent
uses the constructor:
MouseWheelEvent
(Component src, int type, long when, int modifier, int x,int y, int clicks,
boolean triggersPopup, int scrollHow, int amont, int count)
Here, src is a
reference to the object that generated this event. The type of the event is
specified by type. The system time at which the mouse event occurred is passed
in when. The modifier argument specifies which modifiers were pressed when this
mouse event occurred. The coordinates of the mouse are passed in x and y. The
number of clicks the wheel has rotated is passed in clicks. The triggerPopup
flag indicates if this event causes a popup menu to appear on this platform.
The scrollHow value must be either WHEEL_UNIT_SCROLL
or WHEEL_BLOCK_SCROLL. The
number of units to scroll is passed in amount. The count parameter indicates
the number of rotational units that the wheel moved.
The
getWheelEvent() method returns number
of rotational units:
int
getWheelRotation()
A positive
value is returned if the wheel moved counterclockwise. The negative value is
returned if the wheel moved clockwise.
The
getScrollType() method returns either
WHEEL_UNIT_SCROLL or WHEEL_BLOCK_SCROLL.
int
getScrollType ()
If WHEEL_UNIT_SCROLL is returned getScrollAmount() can be called to
obtain the number of units scrolled:
int
getScrollAmount()
The TextEvent
Class:
TextEvents
are defines the integer constant TEXT_VALUE_CHANGED.
TextEvent
has the constructor:
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
text event object does not include the characters currently in the text
components that generated the event.
The WindowEvent
Class:
WindowEvent
class defines the following integer constants that can be used to identify them:
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
class is a subclass of ComponentEvent. It uses the constructor:
WindowEvent (Window
src, int type)
WindowEvent
(Window src, int type, Window other)
WindowEvent
(Window src, int type, int fromState, int toState)
WindowEvent
(Window src, int type, Window other, int fromState, int toState)
Here, src is a
reference to the object that generated this event. The type of the event is
specified by type. other specifies the opposite window when a focus or
activation event occurs. The fromState specifies the prior state of the window,
and the toState specifies the next state of the window.
The
getWindow() method returns the object
that generated the event.
Window getWindow()
WindowEvent
also defines methods that returns the opposite window (When a focus or
activation event has occurred), the previous window state and the current
window state:
Window
getOppositeWindow()
int
getOldState()
int
getNewState()
No comments:
Write comments