Java Swing Focus setFocusOrder(JComponent... components)

Here you can find the source of setFocusOrder(JComponent... components)

Description

set Focus Order

License

Open Source License

Declaration

static public void setFocusOrder(JComponent... components) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Costantino Cerbo./*from w  w  w  . j a  v  a 2 s  .  c  o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Costantino Cerbo - initial API and implementation
 ******************************************************************************/

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public class Main {
    static public void setFocusOrder(JComponent... components) {
        for (int i = 0; i < components.length - 1; i++) {
            setNextFocusable(components[i], components[i + 1]);
        }
        setNextFocusable(components[components.length - 1], components[0]);
    }

    static public void setNextFocusable(JComponent component, final JComponent nextFocusable) {
        component.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "setNextFocusable");
        component.getActionMap().put("setNextFocusable", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                nextFocusable.requestFocusInWindow();
            }
        });
    }
}

Related

  1. selectAllOnFocusGained()
  2. selectAndFocus(JComponent component)
  3. setFocus(JComponent component)
  4. setFocusableRecursively(final JComponent component, final boolean focusable, final boolean childsOnly)
  5. setFocusOn(Component component)
  6. setNextFocusable(JComponent component, final JComponent nextFocusable)
  7. smartRequestFocus(Component component)