Java Reference
In-Depth Information
The
KeyListener
interface
Defines methods to respond to events arising when a key on the keyboard is pressed or released.
Defined Methods
Description
keyTyped(KeyEvent e)
Called when a key on the keyboard is pressed
then released.
keyPressed(KeyEvent e)
Called when a key on the keyboard is pressed.
keyReleased(KeyEvent e)
Called when a key on the keyboard is released.
The
FocusListener
interface
Defines methods to respond to a component gaining or losing the focus. You might implement these
methods to change the appearance of the component to reflect whether or not it has the focus.
Defined Methods
Description
focusGained(FocusEvent e)
Called when a component gains the keyboard
focus.
focusLost(FocusEvent e)
Called when a component loses the keyboard
focus.
There is a further listener interface,
MouseInputListener
, which is defined in the
javax.swing.event
package. This listener implements both the
MouseListener
and
MouseMotionListener
interfaces so it declares methods for all possible mouse events in a
single interface.
The
WindowListener
,
WindowFocusListener
, and
WindowStateListener
interfaces between
them define methods corresponding to each of the event IDs defined in the
WindowEvent
class that we
saw earlier. If you deduced from this that the methods in the other listener interfaces correspond to
event IDs for the other event classes, well, you're right. All the IDs for mouse events are defined in the
MouseEvent
class. These are:
MOUSE
_
CLICKED
MOUSE
_
PRESSED
MOUSE
_
DRAGGED
MOUSE
_
ENTERED
MOUSE
_
EXITED
MOUSE
_
RELEASED
MOUSE
_
MOVED
MOUSE
_
WHEEL
The
MOUSE
_
MOVED
event corresponds to just moving the mouse. The
MOUSE
_
DRAGGED
event arises
when you move the mouse while keeping a button pressed.
The event IDs defined in the
KeyEvent
class are:
KEY
_
TYPED
KEY
_
PRESSED
KEY
_
RELEASED