Example usage for java.awt Component getComponentAt

List of usage examples for java.awt Component getComponentAt

Introduction

In this page you can find the example usage for java.awt Component getComponentAt.

Prototype

public Component getComponentAt(int x, int y) 

Source Link

Document

Determines if this component or one of its immediate subcomponents contains the (xy) location, and if so, returns the containing component.

Usage

From source file:Main.java

/**
 * Returns top component inside the specified container component at the
 * specified point./*  w  w  w .j a  v  a2s  .  co m*/
 *
 * @param component
 *            container component to process
 * @param x
 *            X coordinate
 * @param y
 *            Y coordinate
 * @return top component inside the specified container component at the
 *         specified point
 */
public static Component getTopComponentAt(final Component component, final int x, final int y) {
    final Component child = component.getComponentAt(x, y);
    if (child == component || !(child instanceof Container)) {
        return component;
    } else {
        final Rectangle b = child.getBounds();
        return getTopComponentAt(child, x - b.x, y - b.y);
    }
}