Java Swing Focus requestFocus(final Window win, final Component comp)

Here you can find the source of requestFocus(final Window win, final Component comp)

Description

Focuses on the specified component as soon as the window becomes active.

License

Open Source License

Parameter

Parameter Description
win The window
comp The component

Declaration

public static void requestFocus(final Window win, final Component comp) 

Method Source Code


//package com.java2s;
/*/*  w w w. j av a  2  s .  c o m*/
 * GUIUtilities.java - Various GUI utility functions
 * :tabSize=8:indentSize=8:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 1999, 2004 Slava Pestov
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Component;

import java.awt.Window;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.SwingUtilities;

public class Main {
    /**
     * Focuses on the specified component as soon as the window becomes
     * active.
     * @param win The window
     * @param comp The component
     */
    public static void requestFocus(final Window win, final Component comp) {
        win.addWindowListener(new WindowAdapter() {
            public void windowActivated(WindowEvent evt) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        comp.requestFocus();
                    }
                });
                win.removeWindowListener(this);
            }
        });
    }
}

Related

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