* @param toList
* Move into this List
* @return Moved Elements
*/
public static Object[] moveSelectedItems(JList fromList, JList toList) {
DefaultListModel fromModel = (DefaultListModel) fromList.getModel();
DefaultListModel toModel = (DefaultListModel) toList.getModel();
// get the selection
int[] selection = fromList.getSelectedIndices();
if (selection.length == 0) {
return new Object[] {};
}
Object[] objects = new Object[selection.length];
for (int i = 0; i < selection.length; i++) {
objects[i] = fromModel.getElementAt(selection[i]);
}
// get the target insertion position
int targetPos = toList.getMaxSelectionIndex();
if (targetPos == -1) {
targetPos = toModel.getSize();
} else {
targetPos++;
}
// suppress updates on both lists
if (selection.length >= 5) {
fromList.setModel(new DefaultListModel());
toList.setModel(new DefaultListModel());
}
// move the elements
for (int i = selection.length - 1; i >= 0; i--) {
Object value = fromModel.remove(selection[i]);
toModel.add(targetPos, value);
}
if (selection.length >= 5) {
fromList.setModel(fromModel);
toList.setModel(toModel);