Java Swing Focus focusOnOpen(final JComponent component)

Here you can find the source of focusOnOpen(final JComponent component)

Description

focus On Open

License

Open Source License

Declaration

public static void focusOnOpen(final JComponent component) 

Method Source Code


//package com.java2s;
/*//from  w ww  . j a  v  a  2s  . c o  m
(C) 2007 Stefan Reich (jazz@drjava.de)
This source file is part of Project Prophecy.
For up-to-date information, see http://www.drjava.de/prophecy
    
This source file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
*/

import javax.swing.*;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;

public class Main {
    public static void focusOnOpen(final JComponent component) {
        /*
        final Window window = SwingUtilities.getWindowAncestor(component);
        if (window != null)
          window.addWindowListener(new WindowAdapter() {
        public void windowOpened(WindowEvent e) {
          component.requestFocus();
          window.removeWindowListener(this);
        }
          });
        */

        component.addAncestorListener(new AncestorListener() {
            public void ancestorAdded(AncestorEvent event) {
                component.requestFocusInWindow();
                component.removeAncestorListener(this);
            }

            public void ancestorRemoved(AncestorEvent event) {
            }

            public void ancestorMoved(AncestorEvent event) {
            }
        });
    }
}

Related

  1. focusComponentOrChild(Component c, boolean deepest)
  2. focusFirstFocusableChild(Container c)
  3. focusFirstFocusableComponent(Component c)
  4. focusLater(final Component component)
  5. focusLater(final Component component)
  6. getFocusableComponentOrChild(Component c, boolean deepest)
  7. getFocusableComponentOrChild(Component c, boolean deepest)
  8. getPermanentFocusOwner()
  9. hasFocus(Component component)