Java Swing KeyStroke fixTabKeys(final JComponent component)

Here you can find the source of fixTabKeys(final JComponent component)

Description

This method sets the FocusTraversalKeys for a component to be the standard keys.

License

Open Source License

Parameter

Parameter Description
component the component that you want to fix tab keys for

Declaration

public static void fixTabKeys(final JComponent component) 

Method Source Code


//package com.java2s;
// Licensed under the MIT license. See License.txt in the project root.

import javax.swing.JComponent;

import javax.swing.KeyStroke;

import java.awt.AWTKeyStroke;

import java.awt.KeyboardFocusManager;
import java.util.HashSet;
import java.util.Set;

public class Main {
    /**/*from   w  w  w.  j av a  2s  .com*/
     * This method sets the FocusTraversalKeys for a component to be the standard keys.
     * Use this on Tables or TextAreas where you want the tab keys to leave the control.
     *
     * @param component the component that you want to fix tab keys for
     */
    public static void fixTabKeys(final JComponent component) {
        final Set<AWTKeyStroke> forward = new HashSet<AWTKeyStroke>(
                component.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        forward.add(KeyStroke.getKeyStroke("TAB"));
        component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forward);
        final Set<AWTKeyStroke> backward = new HashSet<AWTKeyStroke>(
                component.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
        backward.add(KeyStroke.getKeyStroke("shift TAB"));
        component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backward);
    }
}

Related

  1. componentListensForKey(JComponent component, int keyCode)
  2. configAction(Action action, String text, Icon icon, KeyStroke keyStroke)
  3. convertShortcutMask(KeyStroke ks, int shortcutMask)
  4. createItem(String name, Integer mnem, KeyStroke accel)
  5. createKeyMask(final int aKeyStroke, final int... aMasks)
  6. getAccelerator(final int key)
  7. getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke)
  8. getKeyStroke(String s)
  9. getKeyStrokeWithoutCtrlModifier(KeyStroke stroke)