public void actionPerformed(ActionEvent e) { /* * This method can be called only if * there's a valid selection, * so go ahead and remove whatever's selected. */ ListSelectionModel lsm = list.getSelectionModel(); int firstSelected = lsm.getMinSelectionIndex(); int lastSelected = lsm.getMaxSelectionIndex(); listModel.removeRange(firstSelected, lastSelected); int size = listModel.size(); if (size == 0) { // List is empty: disable delete, up, and down buttons. deleteButton.setEnabled(false); upButton.setEnabled(false); downButton.setEnabled(false); } else { // Adjust the selection. if (firstSelected == listModel.getSize()) { // Removed item in last position. firstSelected--; } list.setSelectedIndex(firstSelected); } }
public void valueChanged(ListSelectionEvent e) { ListSelectionModel lsm = (ListSelectionModel) e.getSource(); int firstIndex = e.getFirstIndex(); int lastIndex = e.getLastIndex(); boolean isAdjusting = e.getValueIsAdjusting(); output.append( "Event for indexes " + firstIndex + " - " + lastIndex + "; isAdjusting is " + isAdjusting + "; selected indexes:"); if (lsm.isSelectionEmpty()) { output.append(" <none>"); } else { // Find out which indexes are selected. int minIndex = lsm.getMinSelectionIndex(); int maxIndex = lsm.getMaxSelectionIndex(); for (int i = minIndex; i <= maxIndex; i++) { if (lsm.isSelectedIndex(i)) { output.append(" " + i); } } } output.append(newline); output.setCaretPosition(output.getDocument().getLength()); }
/** * Returns the number of columns selected. * * @return the number of columns selected */ public int getSelectedColumnCount() { if (selectionModel != null) { int iMin = selectionModel.getMinSelectionIndex(); int iMax = selectionModel.getMaxSelectionIndex(); int count = 0; for (int i = iMin; i <= iMax; i++) { if (selectionModel.isSelectedIndex(i)) { count++; } } return count; } return 0; }
private List<Node> getSelectedNodes() { List<Node> selectedNodes = new Vector<Node>(); NodeStatusTableModel myTableModel = (NodeStatusTableModel) getModel(); ListSelectionModel lsm = getSelectionModel(); int minIndex = lsm.getMinSelectionIndex(); int maxIndex = lsm.getMaxSelectionIndex(); for (int i = minIndex; i <= maxIndex; i++) { if (lsm.isSelectedIndex(i)) { // System.out.println("row " + i + " is selected"); Node currNode = myTableModel.getNode(i); if (currNode != null) selectedNodes.add(currNode); } } return selectedNodes; }
/** * Returns an array of selected columns. If <code>selectionModel</code> is <code>null</code>, * returns an empty array. * * @return an array of selected columns or an empty array if nothing is selected or the <code> * selectionModel</code> is <code>null</code> */ public int[] getSelectedColumns() { if (selectionModel != null) { int iMin = selectionModel.getMinSelectionIndex(); int iMax = selectionModel.getMaxSelectionIndex(); if ((iMin == -1) || (iMax == -1)) { return new int[0]; } int[] rvTmp = new int[1 + (iMax - iMin)]; int n = 0; for (int i = iMin; i <= iMax; i++) { if (selectionModel.isSelectedIndex(i)) { rvTmp[n++] = i; } } int[] rv = new int[n]; System.arraycopy(rvTmp, 0, rv, 0, n); return rv; } return new int[0]; }