Java JList Select updateSelectionAfterItemMoved(ListSelectionModel selectionModel, int[] previousSelection, int previousIndex, int newIndex)

Here you can find the source of updateSelectionAfterItemMoved(ListSelectionModel selectionModel, int[] previousSelection, int previousIndex, int newIndex)

Description

update Selection After Item Moved

License

Apache License

Declaration

public static void updateSelectionAfterItemMoved(ListSelectionModel selectionModel, int[] previousSelection,
            int previousIndex, int newIndex) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

public class Main {
    public static void updateSelectionAfterItemMoved(ListSelectionModel selectionModel, int[] previousSelection,
            int previousIndex, int newIndex) {

        selectionModel.setValueIsAdjusting(true);
        selectionModel.clearSelection();
        for (int index : previousSelection) {
            int insertion;
            if (index == previousIndex) {
                insertion = newIndex;/*from w  ww.  ja  va 2s.  c o m*/
            } else {
                insertion = index;
                if (index > previousIndex) {
                    insertion--;
                }
                if (index > newIndex) {
                    insertion++;
                }
            }
            selectionModel.addSelectionInterval(insertion, insertion);
        }
        selectionModel.setValueIsAdjusting(false);
    }
}

Related

  1. setSelectedList(JList list, Object selected)
  2. setSelectedListIndices(final JList list, final List indices)
  3. setSelectedValue(JList list, Object value)
  4. setSelectedValues(JList jList, Object[] values)
  5. setSelection(JList list, Object element)