question: private int insertIndexForLayer ( Component comp , int layer , int position ) { int i , count , curLayer ; int layerStart = - 1 ; int layerEnd = - 1 ; int componentCount = getComponentCount ( ) ; ArrayList < Component > compList = new ArrayList < Component > ( componentCount ) ; for ( int index = 0 ; index < componentCount ; index ++ ) { if ( getComponent ( index ) != comp ) { compList . add ( getComponent ( index ) ) ; } } count = compList . size ( ) ; for ( i = 0 ; i < count ; i ++ ) { curLayer = getLayer ( compList . get ( i ) ) ; if ( layerStart == - 1 && curLayer == layer ) { layerStart = i ; } if ( curLayer < layer ) { if ( i == 0 ) { layerStart = 0 ; layerEnd = 0 ; } else { layerEnd = i ; } break ; } } if ( layerStart == - 1 && layerEnd == - 1 ) return count ; if ( layerStart != - 1 && layerEnd == - 1 ) layerEnd = count ; if ( layerEnd != - 1 && layerStart == - 1 ) layerStart = layerEnd ; if ( position == - 1 ) return layerEnd ; if ( position > - 1 && layerStart + position <= layerEnd ) return layerStart + position ; return layerEnd ; }
target:  This method is an extended version of insertIndexForLayer() to support setLayer which uses Container.setZOrder which does not remove the component from the containment hierarchy though we need to ignore it when calculating the insertion index.