WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
3.6 Responding to Keyboard Input
parent InputMap
· ·null
3.6.1.2 Constructor
public InputMap( )
The default constructor is the only constructor available. It creates an empty
InputMap
with noparent
.3.6.1.3 Methods
public KeyStroke[] allKeys( )
Return an array of all
KeyStroke
s defined in theInputMap
, either directly or anywhere along theparent
chain. If there are no mappings, this method returns either an empty array ornull
, depending on the history of theInputMap
(s). Each key appears only once even if it overrides another on the parent chain.public void clear( )
Remove all keystroke mappings from this
InputMap
(does not affect any mappings in theparent
chain).public Object get(KeyStroke keyStroke)
Look up the specified
keyStroke
in theInputMap
(and theparent
chain), returning a value that represents the logical action that should be taken in response. If no match is found, returnsnull
. The result is generally used immediately to look up anAction
in theActionMap
of the component that owns thisInputMap
. Convention dictates that the values returned areString
s describing the nature of the action to perform.public KeyStroke[] keys
Return an array of
KeyStroke
s locally defined in thisInputMap
. That is to say, it does not follow theparent
chain. If there are no mappings, this returns either an empty array ornull
, depending on the history of theInputMap
.public void put(KeyStroke keyStroke, Object actionMapKey)
Define a new mapping for the specified
keyStroke
. Future calls to theget( )
method returnactionMapKey
as the logical action associated withkeyStroke
. As suggested by the parameter name,actionMapKey
isintended to be used to look up an
Action
in anActionMap
. By convention, it should be aString
whose value is descriptive of the action. (Appendix B lists the standardActionMap
keys supported by Swing components; your own classes can use these or define their own as appropriate.) Passing anull actionMapKey
has the same effect as callingremove(keyStroke)
.public void remove(KeyStroke keyStroke)
Remove the mapping defined for the specified
keyStroke
from thisInputMap
. Looking up thatkeyStroke
in the future returns a value that is determined by theparent
chain (which is not affected by this method). If you want to "block" a mapping in a sharedInputMap
that is part of yourparent
chain, define a mapping for thatKeyStroke
to the stringnone
. By convention, there is never anAction
associated withnone
in anyActionMap
, so its presence in yourInputMap
causes theparent
check to be skipped without allowing any action to take place.public int size( )
Return the number of mappings defined in this
InputMap
(not counting any that might be defined in theparent
chain). For a new or newly clearedInputMap
, this returns0
.3.6.2 The ActionMap Class
ActionMap
is responsible for mapping logical action names to concreteAction
instances that carry them out. When the user types a key combination, it's looked up in theInputMap
of a component, and the result is looked up as a key in the correspondingActionMap
.Although any object can be used as a key in an
ActionMap
, in practice they areString
s. By convention, their content is a descriptive name for the action to be performed (such ascopy
,save
, or the like), often obtained by callinggetName( )
on the correspondingAction
.ActionMap
s can be chained together so that common functionality can be shared in a basicActionMap
; specialized components can add unique actions to their ownActionMap
and delegate the common cases to the shared map through theparent
property.A component's
ActionMap
can also be used to configure auditory cues to be played at appropriate points by the component, as described in Chapter 26.3.6.2.1 Property
The single property defined by
ActionMap
is shown in Table 3-11. Theparent
property establishes a fallbackActionMap
that is consulted if an action name is not found in the current map, much as the inheritance chain is followed when looking up the members of Java classes. If you create a cycle in theparent
chain (for example, by setting twoActionMaps
to beparent
s of each other), many of the method calls crash with aStackOverflowError
.Table 3-11. ActionMap property
Property Data type get is set Default value
parent ActionMap
· ·null
3.6.2.2 Constructor
public ActionMap( )
The default constructor is the only constructor available. It creates an empty
ActionMap
with noparent
.3.6.2.3 Methods
public Object[] allKeys( )
Return an array of all logical action names defined in the
ActionMap
, either directly or anywhere along theparent
chain. If there are no mappings, this method returns either an empty array ornull
, depending on the history of theActionMap
(s). Each key appears only once even if it overrides another on the parent chain.public void clear( )
Remove all action mappings from the local
ActionMap
(does not affect any in theparent
chain).public Action get(Object key)
Look up the specified action name in the
ActionMap
(and theparent
chain), returning the correspondingAction
to be executed. If no match is found, returnsnull
. The keys are often obtained by looking up aKeyStroke
in anInputMap
.public Object[] keys
Return an array of the logical action names locally defined in this
ActionMap
. That is to say, it does not follow theparent
chain. If there are no mappings, this returns either an empty array ornull
, depending on the history of theActionMap
.public void put(Object key, Action action)
Define a new mapping for the specified
action
. Future calls to theget( )
method return it as theAction
associated with the logical namekey
. By convention,key
should be aString
whose value is descriptive of the action. Appendix B lists the standardActionMap
keys supported by Swing components; your own classes can use these as well, or define their own, as appropriate. Passing anull key
has the same effect as callingremove(action)
.public void remove(Object Key)
Remove the mapping defined for the specified
key
from thisActionMap
. Looking up that logical action name in the future returns a value determined by theparent
chain (which is not affected by this method).public int size( )
Return the number of mappings defined in this
ActionMap
(not counting any that might be defined in theparent
chain). For a new or newly clearedActionMap
, this method returns0
.I l@ve RuBoard
I l@ve RuBoard