D
dalamb
As part of exploring JWS I wrote a small text editor, which basically
just uses a JTextArea for editing and adds some JNLP-based file
reading and writing. I created some menus for actions of my own, like
"capitalize every word in the selected text" and it all worked fine.
Then I thought about adding menu items for things like "delete" and
"Select all" -- which JTextArea already implements. I found out about
JTextComponent.getActions(), invoked that, and dumped information
about what actions were available. The Action objects appeared not to
have any information for properties other than Action.NAME; in
particular, I got null for getValue(Action.ACCELERATOR_KEY), which
should have been the keystroke for invoking the action.
I then inspected the keybindings in the JTextArea's keymap. The basic
code looked like:
public static void showKeymap(JTextComponent component) {
Keymap map = component.getKeymap();
if (map==null) System.err.println("no key map");
int maps=0;
while (map != null) {
KeyStroke[] keys = map.getBoundKeyStrokes();
System.err.println("Map "+(maps++)+" "+keys.length+" keys.");
// ... stuff to loop over keys and print information about them
// Process all parent keymaps as well
map = map.getResolveParent();
} // end while
} // end showKeymap
I found out that there was a basic map and one ancestor -- but neither
had any bound keystrokes (keys.length was 0). But obviously various
keystrokes really do something useful in a JTextComponent -- ctrl-A
does select all, for example.
So is there some other way to programmatically find out what key
bindings (and corresponding actions) a JTextComponent supports?
Appendix: for the curious, here's a list of actions provided by
getActions():
'beep' 'caret-backward' 'caret-begin' 'caret-begin-line'
'caret-begin-paragraph' 'caret-begin-word' 'caret-down' 'caret-end'
'caret-end-line' 'caret-end-paragraph' 'caret-end-word'
'caret-forward' 'caret-next-word' 'caret-previous-word' 'caret-up'
'copy-to-clipboard' 'cut-to-clipboard' 'default-typed' 'delete-next'
'delete-next-word' 'delete-previous' 'delete-previous-word'
'dump-model' 'insert-break' 'insert-content' 'insert-tab' 'page-down'
'page-up' 'paste-from-clipboard' 'select-all' 'selection-backward'
'selection-begin' 'selection-begin-line' 'selection-begin-paragraph'
'selection-begin-word' 'selection-down' 'selection-end'
'selection-end-line' 'selection-end-paragraph' 'selection-end-word'
'selection-forward' 'selection-next-word' 'selection-page-down'
'selection-page-left' 'selection-page-right' 'selection-page-up'
'selection-previous-word' 'selection-up' 'select-line'
'select-paragraph' 'select-word' 'set-read-only' 'set-writable'
'toggle-componentOrientation' 'unselect'
just uses a JTextArea for editing and adds some JNLP-based file
reading and writing. I created some menus for actions of my own, like
"capitalize every word in the selected text" and it all worked fine.
Then I thought about adding menu items for things like "delete" and
"Select all" -- which JTextArea already implements. I found out about
JTextComponent.getActions(), invoked that, and dumped information
about what actions were available. The Action objects appeared not to
have any information for properties other than Action.NAME; in
particular, I got null for getValue(Action.ACCELERATOR_KEY), which
should have been the keystroke for invoking the action.
I then inspected the keybindings in the JTextArea's keymap. The basic
code looked like:
public static void showKeymap(JTextComponent component) {
Keymap map = component.getKeymap();
if (map==null) System.err.println("no key map");
int maps=0;
while (map != null) {
KeyStroke[] keys = map.getBoundKeyStrokes();
System.err.println("Map "+(maps++)+" "+keys.length+" keys.");
// ... stuff to loop over keys and print information about them
// Process all parent keymaps as well
map = map.getResolveParent();
} // end while
} // end showKeymap
I found out that there was a basic map and one ancestor -- but neither
had any bound keystrokes (keys.length was 0). But obviously various
keystrokes really do something useful in a JTextComponent -- ctrl-A
does select all, for example.
So is there some other way to programmatically find out what key
bindings (and corresponding actions) a JTextComponent supports?
Appendix: for the curious, here's a list of actions provided by
getActions():
'beep' 'caret-backward' 'caret-begin' 'caret-begin-line'
'caret-begin-paragraph' 'caret-begin-word' 'caret-down' 'caret-end'
'caret-end-line' 'caret-end-paragraph' 'caret-end-word'
'caret-forward' 'caret-next-word' 'caret-previous-word' 'caret-up'
'copy-to-clipboard' 'cut-to-clipboard' 'default-typed' 'delete-next'
'delete-next-word' 'delete-previous' 'delete-previous-word'
'dump-model' 'insert-break' 'insert-content' 'insert-tab' 'page-down'
'page-up' 'paste-from-clipboard' 'select-all' 'selection-backward'
'selection-begin' 'selection-begin-line' 'selection-begin-paragraph'
'selection-begin-word' 'selection-down' 'selection-end'
'selection-end-line' 'selection-end-paragraph' 'selection-end-word'
'selection-forward' 'selection-next-word' 'selection-page-down'
'selection-page-left' 'selection-page-right' 'selection-page-up'
'selection-previous-word' 'selection-up' 'select-line'
'select-paragraph' 'select-word' 'set-read-only' 'set-writable'
'toggle-componentOrientation' 'unselect'