Returns whether the parent window of a JComponent has the current focus (or any other component in the window). - Java Swing

Java examples for Swing:JWindow

Description

Returns whether the parent window of a JComponent has the current focus (or any other component in the window).

Demo Code


import java.awt.*;
import javax.swing.*;

public class Main{
    /**/*from  w  w w  .  java2s.c o m*/
     * Returns whether the parent window of a component has the current focus (or
     * any other component in the window).
     * @param component The component for which the parent window is searched.
     * @return True if the parent window currently owns the focus, false otherwise.
     **/
    static public boolean parentWindowHasFocus(JComponent component) {
        Window windowFocusOwner = FocusUtils.focusedWindow();
        if (windowFocusOwner == null)
            return false;
        return component.getTopLevelAncestor() == windowFocusOwner;
    }
    /**
     * Returns the window that is currently the focus owner. May be null if no
     * window owns the focus.
     * @return The window that currently owns the focus, or null.
     */
    static public Window focusedWindow() {
        return KeyboardFocusManager.getCurrentKeyboardFocusManager()
                .getFocusedWindow();
    }
}

Related Tutorials