Java examples for Swing:JComponent
Determines if the component is visible in its window at the given screen location.
//package com.java2s; import java.awt.Component; import java.awt.Container; import java.awt.Point; import java.awt.Window; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JRootPane; import javax.swing.JWindow; import javax.swing.SwingUtilities; public class Main { /**/*from w w w . j a v a2s . c o m*/ * <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); } /** * Gets the root pane of the window. * The window should be a javax.swing.JFrame, javax.swing.JDialog * or javax.swing.JWindow. Otherwise null is returned. * * @param window The window whose root pane is retrieved. * @return The root pane of the window of the component. */ public static JRootPane getRootPane(Window window) { if (window == null) { return null; } // Get the root pane if we can find one. if (window instanceof JFrame) return ((JFrame) window).getRootPane(); if (window instanceof JWindow) return ((JWindow) window).getRootPane(); if (window instanceof JDialog) return ((JDialog) window).getRootPane(); // We could not find a root pane for this window. return null; } /** * Gets the content pane of the given window. * * @param window The window. It should inherit from javax.swing.JFrame, * javax.swing.JDialog or javax.swing.JWindow. * @return The content pane of the window. * If the given window is not a javax.swing.JFrame, * javax.swing.JDialog or javax.swing.JWindow, null is returned. */ public static Container getContentPane(Window window) { // Get the layered pane if we can find one. if (window instanceof JFrame) return ((JFrame) window).getContentPane(); if (window instanceof JDialog) return ((JDialog) window).getContentPane(); if (window instanceof JWindow) return ((JWindow) window).getContentPane(); // We could not find a root pane for this window. return null; } }