int gridY2 = Math.min(location.y / cellHeight, visibleRows2);
int gridY2 = Math.min(location.y / maxCellHeight2, visibleRows2);
  public int locationToIndex(JList list, Point location)  {    int layoutOrientation = list.getLayoutOrientation();    int index = -1;    switch (layoutOrientation)      {      case JList.VERTICAL:        index = convertYToRow(location.y);        break;      case JList.HORIZONTAL_WRAP:        // determine visible rows and cells per row        int visibleRows = list.getVisibleRowCount();        int cellsPerRow = -1;        int numberOfItems = list.getModel().getSize();        Dimension listDim = list.getSize();        if (visibleRows <= 0)          {            try              {                cellsPerRow = listDim.width / cellWidth;              }            catch (ArithmeticException ex)              {                cellsPerRow = 1;              }          }        else          {            cellsPerRow = numberOfItems / visibleRows + 1;          }        // determine index for the given location        int cellsPerColumn = numberOfItems / cellsPerRow + 1;        int gridX = Math.min(location.x / cellWidth, cellsPerRow - 1);        int gridY = Math.min(location.y / cellHeight, cellsPerColumn);        index = gridX + gridY * cellsPerRow;        break;      case JList.VERTICAL_WRAP:        // determine visible rows and cells per column        int visibleRows2 = list.getVisibleRowCount();        if (visibleRows2 <= 0)          {            Dimension listDim2 = list.getSize();            visibleRows2 = listDim2.height / cellHeight;          }        int numberOfItems2 = list.getModel().getSize();        int cellsPerRow2 = numberOfItems2 / visibleRows2 + 1;        int gridX2 = Math.min(location.x / cellWidth, cellsPerRow2 - 1);        int gridY2 = Math.min(location.y / cellHeight, visibleRows2);        index = gridY2 + gridX2 * visibleRows2;        break;      }    return index;  }