Java Swing Focus installJComponentRepainterOnWindowFocusChanged(JComponent component)

Here you can find the source of installJComponentRepainterOnWindowFocusChanged(JComponent component)

Description

Installs a listener on the given JComponent 's parent Window that repaints the given component when the parent window's focused state changes.

License

LGPL

Parameter

Parameter Description
component the JComponent to add the repaint focus listener to.

Declaration

public static void installJComponentRepainterOnWindowFocusChanged(JComponent component) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.lang.ref.WeakReference;
import javax.swing.FocusManager;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class Main {
    /**/*from w  w w .  j av a2 s  . c o m*/
     * Installs a listener on the given {@link JComponent}'s parent {@link Window}
     * that repaints the given component when the parent window's focused state
     * changes. If the given component does not have a parent at the time this
     * method is called, then an ancestor listener will be installed that installs
     * a window listener when the components parent changes.
     * 
     * @param component
     *          the {@code JComponent} to add the repaint focus listener to.
     */
    public static void installJComponentRepainterOnWindowFocusChanged(JComponent component) {
        // TODO check to see if the component already has an ancestor.
        WindowFocusListener windowListener = createWeakWindowFocusListener(createRepaintWindowListener(component));
        AncestorListener ancestorListener = createAncestorListener(component, windowListener);
        component.addAncestorListener(ancestorListener);
    }

    private static WindowFocusListener createWeakWindowFocusListener(WindowFocusListener windowFocusListener) {
        final WeakReference<WindowFocusListener> weakReference = new WeakReference<WindowFocusListener>(
                windowFocusListener);
        return new WindowFocusListener() {
            public void windowGainedFocus(WindowEvent e) {
                // TODO if the WeakReference's object is null, remove the WeakReference
                // as a
                // TODO FocusListener.
                if (weakReference.get() != null) {
                    weakReference.get().windowGainedFocus(e);
                }
            }

            public void windowLostFocus(WindowEvent e) {
                if (weakReference.get() != null) {
                    weakReference.get().windowLostFocus(e);
                }
            }
        };
    }

    /**
     * Creates a {@link WindowFocusListener} that calls repaint on the given
     * {@link JComponent} when focused gained or focus lost is called.
     */
    private static WindowFocusListener createRepaintWindowListener(final JComponent component) {
        if (component instanceof WindowFocusListener) {
            return (WindowFocusListener) component;
        } else {
            return new WindowFocusListener() {
                public void windowGainedFocus(WindowEvent e) {
                    if (component instanceof WindowFocusListener) {
                        ((WindowFocusListener) component).windowGainedFocus(e);
                    }
                    component.repaint();
                }

                public void windowLostFocus(WindowEvent e) {
                    if (component instanceof WindowFocusListener) {
                        ((WindowFocusListener) component).windowLostFocus(e);
                    }
                    component.repaint();
                }
            };
        }
    }

    /**
     * Creates an {@link AncestorListener} that installs a weakly referenced
     * version of the given {@link WindowFocusListener} when the given
     * {@link JComponent}'s parent changes.
     */
    private static AncestorListener createAncestorListener(JComponent component,
            final WindowFocusListener windowListener) {
        final WeakReference<JComponent> weakReference = new WeakReference<JComponent>(component);
        return new AncestorListener() {
            public void ancestorAdded(AncestorEvent event) {
                // TODO if the WeakReference's object is null, remove the WeakReference
                // as an
                // TODO AncestorListener.
                final Window window = weakReference.get() == null ? null
                        : SwingUtilities.getWindowAncestor(weakReference.get());
                if (window != null) {
                    window.removeWindowFocusListener(windowListener);
                    window.addWindowFocusListener(windowListener);
                    // notify the listener of the original focus state of the window,
                    // which ensures
                    // that the listener is in sync with the actual window state.
                    fireInitialFocusEvent(windowListener, window);
                }
            }

            private void fireInitialFocusEvent(WindowFocusListener windowListener, Window window) {
                Window focusedWindow = FocusManager.getCurrentManager().getFocusedWindow();
                // fire a fake event to the given listener indicating the actual focus
                // state of the
                // given window.
                if (window == focusedWindow) {
                    windowListener.windowGainedFocus(new WindowEvent(window, WindowEvent.WINDOW_GAINED_FOCUS));
                } else {
                    windowListener.windowGainedFocus(new WindowEvent(window, WindowEvent.WINDOW_LOST_FOCUS));
                }
            }

            public void ancestorRemoved(AncestorEvent event) {
                Window window = weakReference.get() == null ? null
                        : SwingUtilities.getWindowAncestor(weakReference.get());
                if (window != null) {
                    window.removeWindowFocusListener(windowListener);
                }
            }

            public void ancestorMoved(AncestorEvent event) {
                // no implementation.
            }
        };
    }
}

Related

  1. hasFocus(Component component)
  2. hasFocusOwner(final Component component)
  3. installDefaultFocusHandling(Container c)
  4. installFocusCycleRoot(Container c, FocusTraversalPolicy cyclePolicy)
  5. installJComponentRepainterOnWindowFocusChanged( JComponent component)
  6. installJComponentRepainterOnWindowFocusChanged(JComponent component)
  7. isDescendingFrom(Component focusOwner, Component parent)
  8. isFocusable(Component component)
  9. isFocused(Component component)