Java Utililty Methods Swing Focus

List of utility methods to do Swing Focus

Description

The list of methods to do Swing Focus are organized into topic(s).

Method

booleanhasFocus(Component component)
has Focus
Component focusOwner = findFocusOwner(component);
return focusOwner != null;
booleanhasFocusOwner(final Component component)
Returns whether component or any of its children has focus or not.
final Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
return component == focusOwner
        || component instanceof Container && ((Container) component).isAncestorOf(focusOwner);
voidinstallDefaultFocusHandling(Container c)
install focus forward and backward
HashSet<KeyStroke> set = new HashSet<KeyStroke>(1);
set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);
set = new HashSet<KeyStroke>(1);
set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);
if (c instanceof JComponent) {
    JComponent jc = (JComponent) c;
...
voidinstallFocusCycleRoot(Container c, FocusTraversalPolicy cyclePolicy)
installs a focus cycle, its keys and the (optional) policy
c.setFocusCycleRoot(true);
c.setFocusTraversalPolicy(cyclePolicy);
if (c instanceof RootPaneContainer) {
    Container contentPane = ((RootPaneContainer) c).getContentPane();
    if (contentPane != null) {
        contentPane.setFocusTraversalPolicy(null);
        contentPane.setFocusCycleRoot(false);
voidinstallJComponentRepainterOnWindowFocusChanged(JComponent component)
Installs a listener on the given JComponent 's parent Window that repaints the given component when the parent window's focused state changes.
WindowListener windowListener = createWeakWindowFocusListener(createRepaintWindowListener(component));
AncestorListener ancestorListener = createAncestorListener(component, windowListener);
component.addAncestorListener(ancestorListener);
voidinstallJComponentRepainterOnWindowFocusChanged(JComponent component)
Installs a listener on the given JComponent 's parent Window that repaints the given component when the parent window's focused state changes.
WindowFocusListener windowListener = createWeakWindowFocusListener(createRepaintWindowListener(component));
AncestorListener ancestorListener = createAncestorListener(component, windowListener);
component.addAncestorListener(ancestorListener);
voidinstallJComponentRepainterOnWindowFocusChanged(JComponent component)
Installs a listener on the given JComponent 's parent Window that repaints the given component when the parent window's focused state changes.
final WindowFocusListener windowListener = createRepaintWindowListener(component);
final AncestorListener ancestorListener = createAncestorListener(component, windowListener);
component.addAncestorListener(ancestorListener);
booleanisDescendingFrom(Component focusOwner, Component parent)
Returns whether the component is part of the parent's container hierarchy.
while (focusOwner != null) {
    if (focusOwner instanceof JPopupMenu) {
        focusOwner = ((JPopupMenu) focusOwner).getInvoker();
        if (focusOwner == null) {
            return false;
    if (focusOwner == parent) {
...
booleanisFocusable(Component component)
Return true if the jComponent could receive the focus
if (component instanceof JTextComponent || component instanceof JComboBox || component instanceof JCheckBox
        || component instanceof JTable) {
    return true;
return false;
booleanisFocused(Component component)
Returns true, if the specified component is focus owner or permanent focus owner and if the component is on an the active window.
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();
...