Java JList Select getSelectedItems(DefaultListModel listModel, ListSelectionModel selectionModel)

Here you can find the source of getSelectedItems(DefaultListModel listModel, ListSelectionModel selectionModel)

Description

get Selected Items

License

Open Source License

Declaration

static <T> List<T> getSelectedItems(DefaultListModel<T> listModel, ListSelectionModel selectionModel) 

Method Source Code


//package com.java2s;
//License from project: GNU General Public License 

import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;
import java.util.ArrayList;
import java.util.List;

public class Main {
    static <T> List<T> getSelectedItems(DefaultListModel<T> listModel, ListSelectionModel selectionModel) {
        int iMin = selectionModel.getMinSelectionIndex();
        int iMax = selectionModel.getMaxSelectionIndex();

        final ArrayList<T> itemList = new ArrayList<>();
        if (iMin < 0 || iMax < 0) {
            return itemList;
        }//w w  w.j  a va 2 s. co m

        for (int i = iMin; i <= iMax; i++) {
            if (selectionModel.isSelectedIndex(i)) {
                itemList.add(listModel.get(i));
            }
        }

        return itemList;
    }
}

Related

  1. ensureSelectionIsVisible(final JList list)
  2. fillSelectionListFromString(JList aListComponent, String theList, boolean removeQuotes)
  3. fireSelectRow(final JList list, final Object value)
  4. fireSelectRows(final JList list, final int[] rows)
  5. getSelectedIndecies(final ListSelectionModel lsm)
  6. getSelectedMultipleList(JList anJList)
  7. getSelectedRows(ListSelectionModel listSelectionModel)
  8. initParamList(final JList paramList, final String[] availNames, final String[] defaultSelection)
  9. JListRemoveSelectedObject(javax.swing.JList list)