Example usage for java.awt KeyboardFocusManager FORWARD_TRAVERSAL_KEYS

List of usage examples for java.awt KeyboardFocusManager FORWARD_TRAVERSAL_KEYS

Introduction

In this page you can find the example usage for java.awt KeyboardFocusManager FORWARD_TRAVERSAL_KEYS.

Prototype

int FORWARD_TRAVERSAL_KEYS

To view the source code for java.awt KeyboardFocusManager FORWARD_TRAVERSAL_KEYS.

Click Source Link

Document

The identifier for the Forward focus traversal keys.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton("a");

    Set set = new HashSet(component.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

    set.add(KeyStroke.getKeyStroke("F2"));
    component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>();

    KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

    set.add(KeyStroke.getKeyStroke("F2"));
    KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton("a");

    Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(
            component.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

    set.add(KeyStroke.getKeyStroke("F2"));
    component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

}

From source file:Main.java

public static void setTabFocusTraversalKeys(final JComponent component) {
    component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
            new HashSet<AWTKeyStroke>(Arrays.asList(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0))));
    component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, new HashSet<AWTKeyStroke>(
            Arrays.asList(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_DOWN_MASK))));
}

From source file:Main.java

/**
 * Disables the forward and backward focus traversal keys on the given
 * component./* w w w. j  a  v  a 2 s .c  om*/
 */
public static void disableFocusTraversal(Component c) {
    Set<AWTKeyStroke> emptySet = Collections.emptySet();
    c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, emptySet);
    c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, emptySet);
}

From source file:Main.java

/**
 * Adds the focus forward key.//ww  w.j ava 2s.  com
 *
 * @param button
 *            the button
 */
public static void addFocusForwardKey(final int button) {
    final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    final Set<?> forwardKeys = manager
            .getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
    final Set<KeyStroke> newForwardKeys = new HashSet(forwardKeys);
    newForwardKeys.add(KeyStroke.getKeyStroke(button, 0));
    manager.setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys);
}

From source file:Main.java

/**
 * install focus forward and backward//from w  w w . j  a  v  a  2  s. c  om
 */
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");
        }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopTextArea.java

@Override
protected JTextArea createTextComponentImpl() {
    final JTextArea impl = new TextAreaFlushableField();

    if (isTabTraversal()) {
        Set<KeyStroke> forwardFocusKey = Collections.singleton(getKeyStroke(KeyEvent.VK_TAB, 0));
        impl.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardFocusKey);

        Set<KeyStroke> backwardFocusKey = Collections
                .singleton(getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_MASK));
        impl.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardFocusKey);

        impl.addKeyListener(new KeyAdapter() {
            @Override//from   w  w  w . ja  v  a 2  s.  c om
            public void keyPressed(KeyEvent e) {
                if (isEnabled() && isEditable() && e.getKeyCode() == KeyEvent.VK_TAB
                        && e.getModifiers() == KeyEvent.CTRL_MASK) {

                    if (StringUtils.isEmpty(impl.getText())) {
                        impl.setText("\t");
                    } else {
                        impl.append("\t");
                    }
                }
            }
        });
    }

    impl.setLineWrap(true);
    impl.setWrapStyleWord(true);

    int height = (int) impl.getPreferredSize().getHeight();
    impl.setMinimumSize(new Dimension(0, height));

    composition = new JScrollPane(impl);
    composition.setPreferredSize(new Dimension(150, height));
    composition.setMinimumSize(new Dimension(0, height));

    doc.putProperty("filterNewlines", false);

    return impl;
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopComponentsHelper.java

/**
 * Make JTable handle TAB key as all other components - move focus to next/previous components.
 * <p>Default Swing behaviour for table is to move focus to next/previous cell inside the table.</p>
 * @param table table instance//from ww w. ja  va 2s . co  m
 */
public static void correctTableFocusTraversal(JTable table) {
    table.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
            Collections.singleton(AWTKeyStroke.getAWTKeyStroke("TAB")));
    table.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
            Collections.singleton(AWTKeyStroke.getAWTKeyStroke("shift TAB")));
}