Here you can find the source of installJComponentRepainterOnWindowFocusChanged( JComponent component)
Parameter | Description |
---|---|
component | the JComponent to add the repaint focus listener to. |
public static void installJComponentRepainterOnWindowFocusChanged( JComponent component)
//package com.java2s; /*/*from w ww.j a v a 2 s . c o m*/ * Copyright (c) 2009 Kathryn Huxtable and Kenneth Orr. * * This file is part of the SeaGlass Pluggable Look and Feel. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * $Id$ */ import java.awt.Window; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.awt.event.WindowListener; import java.lang.ref.WeakReference; import javax.swing.JComponent; import javax.swing.SwingUtilities; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; public class Main { /** * 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. WindowListener windowListener = createWeakWindowFocusListener(createRepaintWindowListener(component)); AncestorListener ancestorListener = createAncestorListener( component, windowListener); component.addAncestorListener(ancestorListener); } private static WindowListener createWeakWindowFocusListener( WindowFocusListener windowFocusListener) { final WeakReference<WindowFocusListener> weakReference = new WeakReference<WindowFocusListener>( windowFocusListener); return new WindowAdapter() { public void windowActivated(WindowEvent e) { // TODO if the WeakReference's object is null, remove the // WeakReference as a FocusListener. if (weakReference.get() != null) { weakReference.get().windowGainedFocus(e); } } public void windowDeactivated(WindowEvent e) { if (weakReference.get() != null) { weakReference.get().windowLostFocus(e); } } }; } private static WindowFocusListener createRepaintWindowListener( final JComponent component) { return new WindowAdapter() { public void windowActivated(WindowEvent e) { component.repaint(); } public void windowDeactivated(WindowEvent e) { component.repaint(); } }; } private static AncestorListener createAncestorListener( JComponent component, final WindowListener 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 AncestorListener. Window window = weakReference.get() == null ? null : SwingUtilities.getWindowAncestor(weakReference .get()); if (window != null) { window.removeWindowListener(windowListener); window.addWindowListener(windowListener); } } public void ancestorRemoved(AncestorEvent event) { Window window = weakReference.get() == null ? null : SwingUtilities.getWindowAncestor(weakReference .get()); if (window != null) { window.removeWindowListener(windowListener); } } public void ancestorMoved(AncestorEvent event) { // no implementation. } }; } }