Java JList Select setSelection(JList list, Object element)

Here you can find the source of setSelection(JList list, Object element)

Description

set Selection

License

Open Source License

Declaration

public static void setSelection(JList list, Object element) 

Method Source Code

//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;
            }
        }
    }
}

Related

  1. setSelectedItems(JList list, List selected)
  2. setSelectedList(JList list, Object selected)
  3. setSelectedListIndices(final JList list, final List indices)
  4. setSelectedValue(JList list, Object value)
  5. setSelectedValues(JList jList, Object[] values)
  6. updateSelectionAfterItemMoved(ListSelectionModel selectionModel, int[] previousSelection, int previousIndex, int newIndex)