List of usage examples for javax.swing JList getModel
public ListModel<E> getModel()
JList
component. From source file:util.ui.UiUtilities.java
/** * Moves Selected Items from one List to another * * @param fromList//from ww w . jav a 2 s. c o m * Move from this List * @param toList * Move into this List * @param row * The target row where to insert * @return Moved Elements */ public static Object[] moveSelectedItems(JList fromList, JList toList, int row) { 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]); } // move the elements for (int i = selection.length - 1; i >= 0; i--) { Object value = fromModel.remove(selection[i]); toModel.insertElementAt(value, row); } // change selection of the fromList if (fromModel.getSize() > 0) { int newSelection = selection[0]; if (newSelection >= fromModel.getSize()) { newSelection = fromModel.getSize() - 1; } // fromList.setSelectedIndex(-1); } // change selection of the toList toList.setSelectionInterval(row, row + selection.length - 1); // ensure the selection is visible toList.ensureIndexIsVisible(toList.getMaxSelectionIndex()); toList.ensureIndexIsVisible(toList.getMinSelectionIndex()); return objects; }
From source file:util.ui.UiUtilities.java
/** * Move selected Items in the JList// www. ja v a 2s . c o m * * @param list * Move Items in this List * @param row * The target row where to insert * @param sort * Dummy parameter, does nothing */ public static void moveSelectedItems(JList list, int row, boolean sort) { DefaultListModel model = (DefaultListModel) list.getModel(); // get the selection int[] selection = list.getSelectedIndices(); if (selection.length == 0) { return; } boolean lower = false; // Remove the selected items Object[] items = new Object[selection.length]; for (int i = selection.length - 1; i >= 0; i--) { if (selection[i] < row && !lower) { row = row - i - 1; lower = true; } items[i] = model.remove(selection[i]); } for (int i = items.length - 1; i >= 0; i--) { model.insertElementAt(items[i], row); } // change selection of the toList list.setSelectionInterval(row, row + selection.length - 1); // ensure the selection is visible list.ensureIndexIsVisible(list.getMaxSelectionIndex()); list.ensureIndexIsVisible(list.getMinSelectionIndex()); }
From source file:util.ui.UiUtilities.java
/** * Move selected Items in the JList//from w ww .j a v a2 s. c o m * * @param list * 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); // ensure the selection is visible list.ensureIndexIsVisible(list.getMaxSelectionIndex()); list.ensureIndexIsVisible(list.getMinSelectionIndex()); }