protected static void removeChildInsidePanel ( JComponent root , JComponent target ) { int N = root . getComponentCount ( ) ; for ( int i = 0 ; i < N ; i ++ ) { try { JPanel p = ( JPanel ) root . getComponent ( i ) ; Component [ ] children = p . getComponents ( ) ; for ( int j = 0 ; j < children . length ; j ++ ) { if ( children [ j ] == target ) { root . remove ( i ) ; return ; } } } catch ( ClassCastException ignore ) { } } }
Searches inside the children of root for a component that s a JPanel . Then inside the JPanel it looks for the target . If the target is inside the JPanel the JPanel is removed from root .