The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.text  [49 examples] > Actions and Key Bindings  [5 examples]

e1005. Listing the Key Bindings in a JTextComponent Keymap

A text component has an additional list of key bindings that is searched after its inputmaps. This additional list is called a keymap. The inputmap mechanism is newer but keymaps were left in place for backwards compatibility.
    JTextArea component = new JTextArea();
    Keymap map = component.getKeymap();
    
    while (map != null) {
        KeyStroke[] keys = map.getBoundKeyStrokes();
    
        for (int i=0; i<keys.length; i++) {
            // This method is defined in e859 Converting a KeyStroke to a String
            String keystrokeStr =  keyStroke2String(keys[i]);
            // Get the action name bound to this keystroke
            Action action = (Action)map.getAction(keys[i]);
        }
    
        // The default action is invoked if a character is typed
        // and no key binding exists in the component's InputMap or Keymap.
        Action defAction = map.getDefaultAction();
    
        // Process all parent keymaps as well
        map = map.getResolveParent();
    }

 Related Examples
e1001. Overriding the Default Action of a JTextComponent
e1002. Creating a Custom Editing Command for a JTextComponent
e1003. Overriding a Few Default Typed Key Bindings in a JTextComponent
e1004. Overriding Many Default Typed Key Bindings in a JTextComponent

See also: Caret and Selection    Events    JEditorPane    JFormattedTextField    JTextArea    JTextComponent    JTextField    JTextPane    Styles   


© 2002 Addison-Wesley.