Java Utililty Methods JList Select

List of utility methods to do JList Select

Description

The list of methods to do JList Select are organized into topic(s).

Method

ListgetSelectedItems(DefaultListModel listModel, ListSelectionModel selectionModel)
get Selected Items
int iMin = selectionModel.getMinSelectionIndex();
int iMax = selectionModel.getMaxSelectionIndex();
final ArrayList<T> itemList = new ArrayList<>();
if (iMin < 0 || iMax < 0) {
    return itemList;
for (int i = iMin; i <= iMax; i++) {
    if (selectionModel.isSelectedIndex(i)) {
...
Object[]getSelectedMultipleList(JList anJList)
fillList
Object[] el = null;
el = anJList.getSelectedValues();
return el;
int[]getSelectedRows(ListSelectionModel listSelectionModel)
get Selected Rows
int iMin = listSelectionModel.getMinSelectionIndex();
int iMax = listSelectionModel.getMaxSelectionIndex();
if ((iMin == -1) || (iMax == -1)) {
    return new int[0];
int[] rvTmp = new int[1 + (iMax - iMin)];
int n = 0;
for (int i = iMin; i <= iMax; i++) {
...
voidinitParamList(final JList paramList, final String[] availNames, final String[] defaultSelection)
init Param List
final List selectedValues = paramList.getSelectedValuesList();
paramList.removeAll();
paramList.setListData(availNames);
paramList.setFixedCellWidth(200);
paramList.setMinimumSize(new Dimension(50, 4));
final int size = paramList.getModel().getSize();
final List<Integer> indices = new ArrayList<>(size);
for (Object selectedValue : selectedValues) {
...
voidJListRemoveSelectedObject(javax.swing.JList list)
J List Remove Selected Object
Object sel = list.getSelectedValue();
if (sel != null) {
    javax.swing.ListModel lm = list.getModel();
    Object[] newList = new Object[lm.getSize() - 1];
    int cur = 0;
    for (int i = 0; i < lm.getSize(); i++) {
        if (lm.getElementAt(i) != sel)
            newList[cur++] = lm.getElementAt(i);
...
voidlinkEnabledToSelected(final JComponent component, final ListSelectionModel model)
Links the enabled state of the specified component to the selected state of the specified ListSelectionModel.
component.setEnabled(!model.isSelectionEmpty());
model.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting())
            component.setEnabled(!model.isSelectionEmpty());
});
...
voidmoveSelectedItems(JList list, int nrRows)
Move selected Items in the JList
DefaultListModel model = (DefaultListModel) list.getModel();
int[] selection = list.getSelectedIndices();
if (selection.length == 0) {
    return;
Object[] items = new Object[selection.length];
for (int i = selection.length - 1; i >= 0; i--) {
    items[i] = model.remove(selection[i]);
...
voidmoveSourceSelection(JList sourceList, boolean moveDown, boolean byPage)
move Source Selection
int size = sourceList.getModel().getSize();
if (size > 0) {
    boolean rollOverEnabled = false;
    int index = sourceList.getSelectedIndex();
    int newIndex = 0;
    if (index >= 0) {
        int maxIndex = size - 1;
        int moveBy = byPage ? sourceList.getVisibleRowCount() : 1;
...
voidremoveListSelectionListeners(final JList comp)
Removes the ListSelection Listeners.
if (comp != null) {
    for (ListSelectionListener l : comp.getListSelectionListeners()) {
        comp.removeListSelectionListener(l);
voidremoveSelectedListItems(JList sourceList)
remove Selected List Items
int sItemSelectedIndex = sourceList.getSelectedIndex();
Object[] sItemSelectedValues = sourceList.getSelectedValues();
DefaultListModel selectedTableListModel = (DefaultListModel) sourceList.getModel();
if (selectedTableListModel.size() > 0) {
    for (int i = sItemSelectedValues.length - 1; i >= 0; i--) {
        selectedTableListModel.removeElement(sItemSelectedValues[i]);
if (sItemSelectedIndex >= 0) {
    if (sItemSelectedIndex >= selectedTableListModel.size()) {
        sourceList.setSelectedIndex(selectedTableListModel.size() - 1);
    } else {
        sourceList.setSelectedIndex(sItemSelectedIndex);