Java JList Select setJListSelection(List selection, JList list)

Here you can find the source of setJListSelection(List selection, JList list)

Description

Sets the selected items on a JList , which is a tedious task to do by hand.

License

Apache License

Parameter

Parameter Description
selection The items that should be selected in the list
list The list itself

Declaration

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void setJListSelection(List selection, JList list) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * //from w ww.  ja  v  a 2  s .com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

import javax.swing.JList;
import javax.swing.ListModel;

public class Main {
    /**
     * Sets the selected items on a {@link JList}, which is a tedious task to do by hand.
     * @param selection The items that should be selected in the list
     * @param list The list itself
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void setJListSelection(List selection, JList list) {

        // Load JList model data in array list
        List modelItems = new ArrayList();
        ListModel listModel = list.getModel();
        for (int i = 0; i < listModel.getSize(); i++) {
            modelItems.add(listModel.getElementAt(i));
        }

        // Determine indices of selection items
        List indices = new ArrayList();
        Iterator items = selection.iterator();
        while (items.hasNext()) {
            Object item = items.next();
            int index = modelItems.indexOf(item);
            if (index != -1) {
                indices.add(new Integer(index));
            }
        }

        // Convert Integer list to int array (man, this is awkward...)
        int[] indicesArr = new int[indices.size()];
        for (int i = 0; i < indices.size(); i++) {
            indicesArr[i] = ((Integer) indices.get(i)).intValue();
        }

        // Set selection
        list.setSelectedIndices(indicesArr);

    }
}

Related

  1. removeSelectionFromList(JList fileList)
  2. removeTypeSelectionListener(JList list)
  3. selectByTyping(javax.swing.JList list, javax.swing.JTextField textfield)
  4. selectRow(int row, ListSelectionModel model)
  5. selectStringInList(String string, JList list)
  6. setLeadAnchorWithoutSelection(ListSelectionModel model, int lead, int anchor)
  7. setSelectedItems(JList list, List selected)
  8. setSelectedList(JList list, Object selected)
  9. setSelectedListIndices(final JList list, final List indices)