List of usage examples for javax.swing DefaultListModel remove
public E remove(int index)
From source file:org.shaman.rpg.editor.dialog.DialogVisualElement.java
private void removeButtonEvent(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonEvent int index = personsList.getSelectedIndex(); if (index < 0) { return; //This should not happen }/* w w w . j ava 2 s . c om*/ @SuppressWarnings("unchecked") DefaultListModel<String> listModel = (DefaultListModel<String>) personsList.getModel(); listModel.remove(index); }
From source file:util.ui.UiUtilities.java
/** * Moves Selected Items from one List to another * * @param fromList//ww w. j av a2s .c om * Move from this List * @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); } // change selection of the fromList if (fromModel.getSize() > 0) { int newSelection = selection[0]; if (newSelection >= fromModel.getSize()) { newSelection = fromModel.getSize() - 1; } fromList.setSelectedIndex(newSelection); } if (selection.length >= 5) { fromList.repaint(); fromList.revalidate(); toList.repaint(); toList.revalidate(); } // change selection of the toList toList.setSelectionInterval(targetPos, targetPos + 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
/** * Moves Selected Items from one List to another * * @param fromList//from w w w . ja v a2 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/*w w w . j a v a 2s . co 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//w w w . j a va2 s. co 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()); }