Java Reference
In-Depth Information
Figure 15-12 shows the screen after typing some information into the text component.
Figure 15-12.
NavigationFilter sample
Keymap Interface
In “MVC-speak,” the
keymap
property of the text component is the Controller part. It maps
KeyStroke
objects to individual actions through the
Keymap
interface. (The
KeyStroke
class
was discussed in Chapter 2.) When you register the
KeyStroke
to the
JTextComponent
with
registerKeyboardAction()
, as in the
PopupSample
program shown in Listing 15-9 earlier in
this chapter, the text component stores this mapping from
KeyStroke
to
Action
in a
Keymap
.
For instance, the Backspace key is mapped to delete the previous character. If you want to
add another binding, you just register another keystroke.
■
Note
In reality, the
Keymap
is just a front to the
ActionMap
/
InputMap
pair. The
JTextComponent
relies on some internal workings to indirectly use the
ActionMap
/
InputMap
classes.
You can also add actions for keystrokes directly to the
Keymap
. This allows you to share a
key map across multiple text components, as long as they all share the same extended behavior.
public interface Keymap {
// Properties
public Action[ ] getBoundActions();
public KeyStroke[ ] getBoundKeyStrokes();
public Action getDefaultAction();
public void setDefaultAction(Action action);
public String getName();
public Keymap getResolveParent();
public void setResolveParent(Keymap parent);
// Other methods
public void addActionForKeyStroke(KeyStroke keystroke, Action action);
public Action getAction(KeyStroke keystroke);
public KeyStroke[ ] getKeyStrokesForAction(Action action);
public boolean isLocallyDefined(KeyStroke keystroke);
public void removeBindings();
public void removeKeyStrokeBinding(KeyStroke keystroke);
}