Java Swing Focus installDefaultFocusHandling(Container c)

Here you can find the source of installDefaultFocusHandling(Container c)

Description

install focus forward and backward

License

Open Source License

Declaration

public static void installDefaultFocusHandling(Container c) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w w .jav a2s  .  co m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.awt.Container;

import java.awt.KeyboardFocusManager;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import java.util.HashSet;

import javax.swing.InputMap;

import javax.swing.JComponent;

import javax.swing.KeyStroke;

public class Main {
    /**
     * install focus forward and backward
     */
    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");
            }
        }
    }
}

Related

  1. getFocusableComponentOrChild(Component c, boolean deepest)
  2. getFocusableComponentOrChild(Component c, boolean deepest)
  3. getPermanentFocusOwner()
  4. hasFocus(Component component)
  5. hasFocusOwner(final Component component)
  6. installFocusCycleRoot(Container c, FocusTraversalPolicy cyclePolicy)
  7. installJComponentRepainterOnWindowFocusChanged( JComponent component)
  8. installJComponentRepainterOnWindowFocusChanged(JComponent component)
  9. installJComponentRepainterOnWindowFocusChanged(JComponent component)