Component parent = c.getParent ();  while (parent instanceof JMenu || parent instanceof JPopupMenu || parent instanceof JMenuItem || parent instanceof JMenuBar) { path.add(0, parent); parent = parent.getParent (); }  return (MenuElement[]) path.toArray();
if (c instanceof JPopupMenu) c = ((JPopupMenu) c).getInvoker(); else c = c.getParent();
  private MenuElement[] getPath (Component c)  {    ArrayList path = new ArrayList();    path.add(0, c);    Component parent = c.getParent ();    while (parent instanceof JMenu            || parent instanceof JPopupMenu            || parent instanceof JMenuItem            || parent instanceof JMenuBar)      {        path.add(0, parent);        parent = parent.getParent ();      }    return (MenuElement[]) path.toArray();  }