< prev index next >

## src/java.desktop/share/classes/javax/swing/JList.java

Print this page

    @@ -2178,28 +2178,11 @@
          * @see #removeSelectionInterval
          * @see #addListSelectionListener
          */
         @Transient
         public int[] getSelectedIndices() {
    -        ListSelectionModel sm = getSelectionModel();
    -        int iMin = sm.getMinSelectionIndex();
    -        int iMax = sm.getMaxSelectionIndex();
    -
    -        if ((iMin < 0) || (iMax < 0)) {
    -            return new int[0];
    -        }
    -
    -        int[] rvTmp = new int[1+ (iMax - iMin)];
    -        int n = 0;
    -        for(int i = iMin; i <= iMax; i++) {
    -            if (sm.isSelectedIndex(i)) {
    -                rvTmp[n++] = i;
    -            }
    -        }
    -        int[] rv = new int[n];
    -        System.arraycopy(rvTmp, 0, rv, 0, n);
    -        return rv;
    +        return getSelectionModel().getSelectedIndices();
         }

         /**
          * Selects a single cell. Does nothing if the given index is greater

    @@ -2262,31 +2245,23 @@
          * @deprecated As of JDK 1.7, replaced by {@link #getSelectedValuesList()}
          */
         @Deprecated
         @BeanProperty(bound = false)
         public Object[] getSelectedValues() {
    -        ListSelectionModel sm = getSelectionModel();
             ListModel<E> dm = getModel();
    +        int[] selectedIndices = getSelectedIndices();
    +        int length = selectedIndices.length;

    -        int iMin = sm.getMinSelectionIndex();
    -        int iMax = sm.getMaxSelectionIndex();
    -
    -        if ((iMin < 0) || (iMax < 0)) {
    -            return new Object[0];
    -        }
    -
    -        Object[] rvTmp = new Object[1+ (iMax - iMin)];
    -        int n = 0;
    -        for(int i = iMin; i <= iMax; i++) {
    -            if (sm.isSelectedIndex(i)) {
    -                rvTmp[n++] = dm.getElementAt(i);
    +        if (length > 0) {
    +            Object[] rv = new Object[length];
    +            for (int i = 0; i < length; i++) {
    +                    rv[i] = dm.getElementAt(selectedIndices[i]);
                 }
    -        }
    -        Object[] rv = new Object[n];
    -        System.arraycopy(rvTmp, 0, rv, 0, n);
             return rv;
         }
    +        return new Object[0];
    +    }

         /**
          * Returns a list of all the selected items, in increasing order based
          * on their indices in the list.
          *

    @@ -2297,28 +2272,22 @@
          *
          * @since 1.7
          */
         @BeanProperty(bound = false)
         public List<E> getSelectedValuesList() {
    -        ListSelectionModel sm = getSelectionModel();
             ListModel<E> dm = getModel();
    +        int[] selectedIndices = getSelectedIndices();

    -        int iMin = sm.getMinSelectionIndex();
    -        int iMax = sm.getMaxSelectionIndex();
    -
    -        if ((iMin < 0) || (iMax < 0)) {
    -            return Collections.emptyList();
    -        }
    -
    +        if (selectedIndices.length > 0) {
             List<E> selectedItems = new ArrayList<E>();
    -        for(int i = iMin; i <= iMax; i++) {
    -            if (sm.isSelectedIndex(i)) {
    +            for (int i : selectedIndices) {
                     selectedItems.add(dm.getElementAt(i));
                 }
    -        }
             return selectedItems;
         }
    +        return Collections.emptyList();
    +    }

         /**
          * Returns the smallest selected cell index; <i>the selection</i> when only
          * a single item is selected in the list. When multiple items are selected,

< prev index next >