< prev index next >

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

Print this page

     315      *
     316      * @param x the ListSelectionListener
     317      * @see #removeListSelectionListener
     318      * @see #setSelectionInterval
     319      * @see #addSelectionInterval
     320      * @see #removeSelectionInterval
     321      * @see #clearSelection
     322      * @see #insertIndexInterval
     323      * @see #removeIndexInterval
     324      */
     325     void addListSelectionListener(ListSelectionListener x);
     326
     327     /**
     328      * Remove a listener from the list that's notified each time a
     329      * change to the selection occurs.
     330      *
     331      * @param x the ListSelectionListener
     332      * @see #addListSelectionListener
     333      */
     334     void removeListSelectionListener(ListSelectionListener x);

     335 }
     315      *
     316      * @param x the ListSelectionListener
     317      * @see #removeListSelectionListener
     318      * @see #setSelectionInterval
     319      * @see #addSelectionInterval
     320      * @see #removeSelectionInterval
     321      * @see #clearSelection
     322      * @see #insertIndexInterval
     323      * @see #removeIndexInterval
     324      */
     325     void addListSelectionListener(ListSelectionListener x);
     326
     327     /**
     328      * Remove a listener from the list that's notified each time a
     329      * change to the selection occurs.
     330      *
     331      * @param x the ListSelectionListener
     332      * @see #addListSelectionListener
     333      */
     334     void removeListSelectionListener(ListSelectionListener x);
     335
     336     /**
     337      * Returns an array of all of the selected indices in the selection model,
     338      * in increasing order.
     339      *
     340      * @return all of the selected indices, in increasing order,
     341      *         or an empty array if nothing is selected
     342      * @see #removeSelectionInterval
     343      * @see #addListSelectionListener
     344      * @since 11
     345      * @implSpec The default implementation iterates from minimum selected
     346      * index {@link #getMinSelectionIndex()} to maximum selected index {@link
     347      * #getMaxSelectionIndex()} and returns the selected indices {@link
     348      * #isSelectedIndex(int)} in a newly allocated int array.
     349      */
     350     default int[] getSelectedIndices() {
     351         int iMin = getMinSelectionIndex();
     352         int iMax = getMaxSelectionIndex();
     353
     354         if ((iMin < 0) || (iMax < 0)) {
     355             return new int[0];
     356         }
     357
     358         int[] rvTmp = new int[1+ (iMax - iMin)];
     359         int n = 0;
     360         for(int i = iMin; i <= iMax; i++) {
     361             if (isSelectedIndex(i)) {
     362                 rvTmp[n++] = i;
     363             }
     364         }
     365         int[] rv = new int[n];
     366         System.arraycopy(rvTmp, 0, rv, 0, n);
     367         return rv;
     368     }
     369
     370     /**
     371      * Returns the number of selected items.
     372      *
     373      * @return the number of selected items, 0 if no items are selected
     374      * @since 11
     375      * @implSpec The default implementation iterates from minimum selected
     376      * index {@link #getMinSelectionIndex()} to maximum selected index {@link
     377      * #getMaxSelectionIndex()} and returns the number of selected indices
     378      * {@link #isSelectedIndex(int)}
     379      */
     380     default int getSelectedItemsCount() {
     381         int iMin = getMinSelectionIndex();
     382         int iMax = getMaxSelectionIndex();
     383         int count = 0;
     384
     385         for(int i = iMin; i <= iMax; i++) {
     386             if (isSelectedIndex(i)) {
     387                 count++;
     388             }
     389         }
     390         return count;
     391     }
     392 }

< prev index next >