int h = viewport.getSize().height; int cellsPerCol = h / cellHeight; int w = size / cellsPerCol * cellWidth; retVal = new Dimension(w, h);
if (visibleRows > 0) {  double modelSize = size; int neededColumns = (int) Math.ceil(modelSize / visibleRows); int adjustedRows = (int) Math.ceil(modelSize / neededColumns); h = maxCellHeight * adjustedRows; w = cellWidth * neededColumns; } else { int neededColumns = Math.min(1, list.getWidth() / cellWidth); h = size / neededColumns * maxCellHeight; w = neededColumns * cellWidth; }
  public Dimension getPreferredSize(JComponent c)  {    int size = list.getModel().getSize();    if (size == 0)      return new Dimension(0, 0);    int visibleRows = list.getVisibleRowCount();    int layoutOrientation = list.getLayoutOrientation();    Rectangle bounds = getCellBounds(list, 0, list.getModel().getSize() - 1);    Dimension retVal = bounds.getSize();    Component parent = list.getParent();    if ((visibleRows == -1) && (parent instanceof JViewport))      {        JViewport viewport = (JViewport) parent;        if (layoutOrientation == JList.HORIZONTAL_WRAP)          {            int h = viewport.getSize().height;            int cellsPerCol = h / cellHeight;            int w = size / cellsPerCol * cellWidth;            retVal = new Dimension(w, h);          }        else if (layoutOrientation == JList.VERTICAL_WRAP)          {            int w = viewport.getSize().width;            int cellsPerRow = Math.max(w / cellWidth, 1);            int h = size / cellsPerRow * cellHeight;            retVal = new Dimension(w, h);          }      }    return retVal;  }