ListCellRenderer rend = list.getCellRenderer(); for (int i = 0; i < nrows; ++i) { Component flyweight = rend.getListCellRendererComponent(list, list.getModel() .getElementAt(i), 0, false, false); Dimension dim = flyweight.getPreferredSize(); cellHeights[i] = dim.height;  cellHeight = (cellHeight * i + cellHeights[i]) / (i + 1); cellWidth = Math.max(cellWidth, dim.width); if (list.getLayoutOrientation() == JList.VERTICAL) cellWidth = Math.max(cellWidth, list.getSize().width); }
cellHeight = fixedCellHeight; cellHeights = null;
  protected void updateLayoutState()  {    int nrows = list.getModel().getSize();    cellHeight = -1;    cellWidth = -1;    if (cellHeights == null || cellHeights.length != nrows)      cellHeights = new int[nrows];    if (list.getFixedCellHeight() == -1 || list.getFixedCellWidth() == -1)      {        ListCellRenderer rend = list.getCellRenderer();        for (int i = 0; i < nrows; ++i)          {            Component flyweight = rend.getListCellRendererComponent(list,                                                                    list.getModel()                                                                        .getElementAt(i),                                                                    0, false,                                                                    false);            Dimension dim = flyweight.getPreferredSize();            cellHeights[i] = dim.height;            // compute average cell height (little hack here)            cellHeight = (cellHeight * i + cellHeights[i]) / (i + 1);            cellWidth = Math.max(cellWidth, dim.width);            if (list.getLayoutOrientation() == JList.VERTICAL)                cellWidth = Math.max(cellWidth, list.getSize().width);          }      }    else      {        cellHeight = list.getFixedCellHeight();        cellWidth = list.getFixedCellWidth();      }  }