Java Swing Focus requestFocusInWindow(final Component c)

Here you can find the source of requestFocusInWindow(final Component c)

Description

Executes requestFocusInWindow on the specified component using invokeLater.

License

Open Source License

Parameter

Parameter Description
c - the component

Declaration

public static void requestFocusInWindow(final Component c) 

Method Source Code

//package com.java2s;
/*//w  w  w  .ja  v  a 2  s  .  com
 * GUIUtils.java
 *
 * Copyright (C) 2002-2015 Takis Diakoumis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.awt.Component;

import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

public class Main {
    /**
     * Executes requestFocusInWindow on the specified component
     * using invokeLater.
     *
     * @param c - the component
     */
    public static void requestFocusInWindow(final Component c) {
        invokeAndWait(new Runnable() {
            public void run() {
                c.requestFocusInWindow();
            }
        });
    }

    /**
     * Runs the specified runnable in the EDT using
     * <code>SwingUtilities.invokeAndWait(...)</code>.
     * Note: This method 'supresses' the method's
     * thrown exceptions - InvocationTargetException and
     * InterruptedException.
     *
     * @param runnable - the runnable to be executed
     */
    public static void invokeAndWait(Runnable runnable) {
        if (!SwingUtilities.isEventDispatchThread()) {
            try {
                //                System.err.println("Not EDT");
                SwingUtilities.invokeAndWait(runnable);
            } catch (InterruptedException e) {
            } catch (InvocationTargetException e) {
            }
        } else {
            runnable.run();
        }
    }
}

Related

  1. requestComponentFocus(final Window win, final JComponent comp)
  2. requestFocus(final JComponent comp)
  3. requestFocus(final JComponent comp)
  4. requestFocus(final JComponent component)
  5. requestFocus(final Window win, final Component comp)
  6. selectAllOnFocusGained()
  7. selectAndFocus(JComponent component)
  8. setFocus(JComponent component)
  9. setFocusableRecursively(final JComponent component, final boolean focusable, final boolean childsOnly)