JComponent has a method
/** * Returns the top-level ancestor of this component (either the * containing <code>Window</code> or <code>Applet</code>), * or <code>null</code> if this component has not * been added to any container. * * @return the top-level <code>Container</code> that this component is in, * or <code>null</code> if not in any container */ public Container getTopLevelAncestor()
so you can compare the containers of both components
source share