Java Swing Focus isFocused(Component component)

Here you can find the source of isFocused(Component component)

Description

Returns true, if the specified component is focus owner or permanent focus owner and if the component is on an the active window.

License

Open Source License

Declaration

public static boolean isFocused(Component component) 

Method Source Code

//package com.java2s;
/*/*from  w w  w .j  a v  a 2  s.co  m*/
 * @(#)QuaquaUtilities.java
 *
 * Copyright (c) 2003-2013 Werner Randelshofer, Switzerland.
 * You may not use, copy or modify this file, except in compliance with the
 * accompanying license terms.
 */

import java.awt.*;

import javax.swing.*;

public class Main {
    /**
     * Returns true, if the specified component is focus owner or permanent
     * focus owner and if the component is on an the active window.
     */
    public static boolean isFocused(Component component) {
        // When a component is used as a cell renderer, the focus can
        // not be determined from the component itself.
        if (component instanceof JComponent) {
            if (((JComponent) component)
                    .getClientProperty("Quaqua.Component.cellRendererFor") != null) {
                component = (Component) ((JComponent) component)
                        .getClientProperty("Quaqua.Component.cellRendererFor");
            }
        }

        KeyboardFocusManager m = KeyboardFocusManager
                .getCurrentKeyboardFocusManager();
        Component c = m.getPermanentFocusOwner();
        if (c == component) {
            Window ancestor = SwingUtilities.getWindowAncestor(component);
            return ancestor != null && ancestor.isFocused();
        } else {
            return false;
        }
    }
}

Related

  1. installJComponentRepainterOnWindowFocusChanged( JComponent component)
  2. installJComponentRepainterOnWindowFocusChanged(JComponent component)
  3. installJComponentRepainterOnWindowFocusChanged(JComponent component)
  4. isDescendingFrom(Component focusOwner, Component parent)
  5. isFocusable(Component component)
  6. isFocused(Component component, boolean recoursive)
  7. isParentWindowFocused(Component component)
  8. isParentWindowFocused(Component component)
  9. isParentWindowFocused(Component component)