Mega Code Archive

 
Categories / Java / Swing JFC
 

Determines if the component is visible in its window at the given screen location

//package com.javadocking.util; import java.awt.Component; import java.awt.Point; import java.awt.Window; import javax.swing.JRootPane; import javax.swing.SwingUtilities; /**  * This class contains a collection of static utility methods for Swing.  *   * @author Heidi Rakels.  */ public class SwingUtil {     /**      * <p>    * Determines if the component is visible in its window at the given screen location.    * </p>    *    * @param   location  A location on the screen.    * @param   component  A component in a window.    * @return        True if the component is visible in its window at the given screen location.    */   public static boolean locationInComponentVisible(Point location, Component component)   {          // Get the root component in the window.     JRootPane rootPane = getRootPane(component);     if (rootPane != null)     {       Component rootComponent = rootPane.getContentPane();       if (rootComponent != null)       {         // Get the location relative to this root component.         Point locationInRoot = new Point(location);         SwingUtilities.convertPointFromScreen(locationInRoot, rootComponent);                  // Get the deepest visible component at the given location.         Component deepestComponent = SwingUtilities.getDeepestComponentAt(rootComponent, locationInRoot.x, locationInRoot.y);         if (deepestComponent != null)         {           boolean result = SwingUtilities.isDescendingFrom(deepestComponent, component);           return result;         }       }     }          return false;        }   /**    * Gets the root pane of the given component.    *     * @param   component    The component whose root pane is retrieved.    * @return           The root pane of the component.    */   public static JRootPane getRootPane(Component component)   {          if (component instanceof JRootPane) {       return (JRootPane)component;     }     if (component.getParent() != null) {       return getRootPane(component.getParent());     }          // Get the window of the component.     Window window = SwingUtilities.windowForComponent(component);     return getRootPane(window);          } }