Here you can find the source of setSelection(JList list, Object element)
public static void setSelection(JList list, Object element)
//package com.java2s; /*/*from w ww. j a v a 2 s .c o m*/ * GeoTools - The Open Source Java GIS Toolkit * http://geotools.org * * (C) 2003-2008, Open Source Geospatial Foundation (OSGeo) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import javax.swing.JList; public class Main { public static void setSelection(JList list, Object element) { for (int i = 0; i < list.getModel().getSize(); i++) { Object value = (Object) list.getModel().getElementAt(i); if (value == element) { list.setSelectedIndex(i); list.scrollRectToVisible(list.getCellBounds(i, i)); return; } } } }