private Dimension calculateElementSize(boolean selected,boolean addMargin){
  if (renderingPrototype != null) {
    Component unselected=renderer.getListCellRendererComponent(this,renderingPrototype,0,selected);
    if (addMargin) {
      return unselected.getPreferredSizeWithMargin();
    }
 else {
      return unselected.getPreferredSize();
    }
  }
  int width=0;
  int height=0;
  int elements=Math.min(listSizeCalculationSampleCount,model.getSize());
  int marginY=0;
  int marginX=0;
  for (int iter=0; iter < elements; iter++) {
    Component cmp=renderer.getListCellRendererComponent(this,model.getItemAt(iter),iter,selected);
    if (cmp instanceof Container) {
      cmp.setShouldCalcPreferredSize(true);
    }
    Dimension d=cmp.getPreferredSize();
    width=Math.max(width,d.getWidth());
    height=Math.max(height,d.getHeight());
    if (iter == 0) {
      Style s=cmp.getStyle();
      marginY=s.getMargin(TOP) + s.getMargin(BOTTOM);
      marginX=s.getMargin(LEFT) + s.getMargin(RIGHT);
    }
  }
  return new Dimension(width + marginX,height + marginY);
}

private Dimension calculateElementSize ( boolean selected , boolean addMargin ) { if ( renderingPrototype != null ) { Component unselected = renderer . getListCellRendererComponent ( this , renderingPrototype , 0 , selected ) ; if ( addMargin ) { return unselected . getPreferredSizeWithMargin ( ) ; } else { return unselected . getPreferredSize ( ) ; } } int width = 0 ; int height = 0 ; int elements = Math . min ( listSizeCalculationSampleCount , model . getSize ( ) ) ; int marginY = 0 ; int marginX = 0 ; for ( int iter = 0 ; iter < elements ; iter ++ ) { Component cmp = renderer . getListCellRendererComponent ( this , model . getItemAt ( iter ) , iter , selected ) ; if ( cmp instanceof Container ) { cmp . setShouldCalcPreferredSize ( true ) ; } Dimension d = cmp . getPreferredSize ( ) ; width = Math . max ( width , d . getWidth ( ) ) ; height = Math . max ( height , d . getHeight ( ) ) ; if ( iter == 0 ) { Style s = cmp . getStyle ( ) ; marginY = s . getMargin ( TOP ) + s . getMargin ( BOTTOM ) ; marginX = s . getMargin ( LEFT ) + s . getMargin ( RIGHT ) ; } } return new Dimension ( width + marginX , height + marginY ) ; }
calculates the size of an element based on a forumla or on rendering prototype
