Java Swing Key Action setUseStandardFocusTraversalKeys(Component comp, boolean use)

Here you can find the source of setUseStandardFocusTraversalKeys(Component comp, boolean use)

Description

Sets whether the given Component uses tab and shift-tab as forward/backward focus traversal keys.

License

Open Source License

Declaration

public static void setUseStandardFocusTraversalKeys(Component comp, boolean use) 

Method Source Code


//package com.java2s;
/*//www.  ja  v a  2s .co  m
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;

public class Main {
    /**
     * Sets whether the given {@code Component} uses tab and shift-tab as
     * forward/backward focus traversal keys.
     */
    public static void setUseStandardFocusTraversalKeys(Component comp, boolean use) {

        modifyFocusTraversalKeys(comp, true, use, KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));

        modifyFocusTraversalKeys(comp, false, use,
                KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK));
    }

    private static void modifyFocusTraversalKeys(Component comp, boolean forward, boolean add,
            AWTKeyStroke... keyStrokes) {

        int dir = forward ? KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
                : KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS;

        Set<AWTKeyStroke> keys = new HashSet<AWTKeyStroke>(comp.getFocusTraversalKeys(dir));

        for (AWTKeyStroke key : keyStrokes) {
            if (add) {
                keys.add(key);
            } else {
                keys.remove(key);
            }
        }

        comp.setFocusTraversalKeys(dir, keys);
    }
}

Related

  1. setEscapeKeyboardAction(final Window window, JComponent pane)
  2. setKeyEvent(JComponent widget, int keyEvent, Runnable action)
  3. setMnemonic(Action action)
  4. setTabFocusTraversalKeys(final JComponent component)
  5. setupAction(Action action, ResourceBundle bundle, String actionId)
  6. simulateEnterKey(Component c)
  7. simulateEnterKeyPressed(final Component component, final int delayInMilliseconds)
  8. simulateKeyStrokes(Component comp, String text)
  9. stringify(KeyStroke keyStroke)