Java tutorial
//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: * 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"); } } } }