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]

e1003. Overriding a Few Default Typed Key Bindings in a JTextComponent

Hitting a key on the keyboard can fire one of three types of key events: KeyEvent.PRESSED, KeyEvent.TYPED, or KeyEvent.RELEASED. Briefly, hitting a key will first fire a PRESSED key event for the key and then, if the key represents an ASCII character (e.g., 'a' or 'control-f'), a TYPED key event containing the character is fired as well. When the key is released, the RELEASED key event is fired.

Rather than have an action and key bindings for every ASCII character, a text component uses a single default action installed in its keymap. When an ASCII character is typed and no key binding exists in any inputmap or keymap for that character, the default action is invoked (which inserts the character into the text component).

Therefore, to override the behavior of a typed character in a text component, you bind a TYPED keystroke to the new action in the text component's inputmap.

There are two ways to override the default key bindings in a JTextComponent. This example demonstrates a technique for overriding a few characters. See e1004 Overriding Many Default Typed Key Bindings in a JTextComponent for a technique suitable for overriding many characters.

    JTextField component = new JTextField(10);
    
    // Override a, A, 9, -, $
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("typed a"), "actionName");
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("typed A"), "actionName");
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("typed 9"), "actionName");
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("typed -"), "actionName");
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("typed $"), "actionName");
    
    // Overriding space must be done this way
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke(new Character(' '), 0), "actionName");
    
    // Disable a character so that no action is invoked.
    // The action name "none" is conventionally used to mean no action.
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("typed X"), "none");
    
    // If you want to bind a keystroke to shift-space (which generates
    // a space character), you need to use a pressed-type keystroke.
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("shift pressed SPACE"), "actionName");
    
    // However, the above is not sufficient. Although it binds the action to shift-space,
    // it does not mask the generated space character. So, not only will the action
    // be invoked, a space character will be inserted into the text component.
    // You need to disable the typed space character.
    // This will prevent the space from being inserted when shift-space is pressed.
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke(new Character(' '), 0), "none");
    
    // But if you still want a non-modified spaced key to insert
    // a space, you need to add a pressed-type keystroke for space.
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("pressed SPACE"), insertSpaceAction.getValue(Action.NAME));
    
    // Add action
    component.getActionMap().put(insertSpaceAction.getValue(Action.NAME), insertSpaceAction);
    
    public Action insertSpaceAction = new AbstractAction("Insert Space") {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent c = (JTextComponent)evt.getSource();
    
            try {
                c.getDocument().insertString(c.getCaretPosition(), " ", null);
            } catch (BadLocationException e) {
            }
        }
    };

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

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


© 2002 Addison-Wesley.