Example usage for java.awt Container setFocusTraversalKeys

List of usage examples for java.awt Container setFocusTraversalKeys

Introduction

In this page you can find the example usage for java.awt Container setFocusTraversalKeys.

Prototype

public void setFocusTraversalKeys(int id, Set<? extends AWTKeyStroke> keystrokes) 

Source Link

Document

Sets the focus traversal keys for a given traversal operation for this Container.

Usage

From source file:Main.java

/**
 * install focus forward and backward// ww w  .  ja  v a  2 s  . c  o m
 */
public static void installDefaultFocusHandling(Container c) {
    // focus TAB
    HashSet<KeyStroke> set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
    c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

    // focus shift-TAB
    set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
    c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);

    // make input map WHEN_FOCUSED non-empty for Focus Traversal policy to work
    // correctly
    if (c instanceof JComponent) {
        JComponent jc = (JComponent) c;
        InputMap inputMapWhenFocused = jc.getInputMap(JComponent.WHEN_FOCUSED);
        if (inputMapWhenFocused.size() == 0) {
            inputMapWhenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_STOP, KeyEvent.KEY_TYPED),
                    "swingDummyFocusKey");
        }
    }
}