* Move Items in this List
* @param nrRows
* Move Items nrRows up/down
*/
public static void moveSelectedItems(JList list, int nrRows) {
DefaultListModel model = (DefaultListModel) list.getModel();
// get the selection
int[] selection = list.getSelectedIndices();
if (selection.length == 0) {
return;
}
// Remove the selected items
Object[] items = new Object[selection.length];
for (int i = selection.length - 1; i >= 0; i--) {
items[i] = model.remove(selection[i]);
}
// insert the elements at the target position
int targetPos = selection[0] + nrRows;
targetPos = Math.max(targetPos, 0);
targetPos = Math.min(targetPos, model.getSize());
for (int i = 0; i < items.length; i++) {
model.add(targetPos + i, items[i]);
}
// change selection of the toList
list.setSelectionInterval(targetPos, targetPos + selection.length - 1);