< prev index next >

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

Print this page

    2163      *
    2164      * @see #setValueIsAdjusting
    2165      * @see ListSelectionModel#getValueIsAdjusting
    2166      */
    2167     public boolean getValueIsAdjusting() {
    2168         return getSelectionModel().getValueIsAdjusting();
    2169     }
    2170
    2171
    2172     /**
    2173      * Returns an array of all of the selected indices, in increasing
    2174      * order.
    2175      *
    2176      * @return all of the selected indices, in increasing order,
    2177      *         or an empty array if nothing is selected
    2178      * @see #removeSelectionInterval
    2179      * @see #addListSelectionListener
    2180      */
    2181     @Transient
    2182     public int[] getSelectedIndices() {
    2183         ListSelectionModel sm = getSelectionModel();
    2184         int iMin = sm.getMinSelectionIndex();
    2185         int iMax = sm.getMaxSelectionIndex();
    2186
    2187         if ((iMin < 0) || (iMax < 0)) {
    2188             return new int[0];
    2189         }
    2190
    2191         int[] rvTmp = new int[1+ (iMax - iMin)];
    2192         int n = 0;
    2193         for(int i = iMin; i <= iMax; i++) {
    2194             if (sm.isSelectedIndex(i)) {
    2195                 rvTmp[n++] = i;
    2196             }
    2197         }
    2198         int[] rv = new int[n];
    2199         System.arraycopy(rvTmp, 0, rv, 0, n);
    2200         return rv;
    2201     }
    2202
    2203
    2204     /**
    2205      * Selects a single cell. Does nothing if the given index is greater
    2206      * than or equal to the model size. This is a convenience method that uses
    2207      * {@code setSelectionInterval} on the selection model. Refer to the
    2208      * documentation for the selection model class being used for details on
    2209      * how values less than {@code 0} are handled.
    2210      *
    2211      * @param index the index of the cell to select
    2212      * @see ListSelectionModel#setSelectionInterval
    2213      * @see #isSelectedIndex
    2214      * @see #addListSelectionListener
    2215      */
    2216     @BeanProperty(bound = false, description
    2217             = "The index of the selected cell.")
    2218     public void setSelectedIndex(int index) {
    2219         if (index >= getModel().getSize()) {
    2220             return;
    2284             }
    2285         }
    2286         Object[] rv = new Object[n];
    2287         System.arraycopy(rvTmp, 0, rv, 0, n);
    2288         return rv;
    2289     }
    2290
    2291     /**
    2292      * Returns a list of all the selected items, in increasing order based
    2293      * on their indices in the list.
    2294      *
    2295      * @return the selected items, or an empty list if nothing is selected
    2296      * @see #isSelectedIndex
    2297      * @see #getModel
    2298      * @see #addListSelectionListener
    2299      *
    2300      * @since 1.7
    2301      */
    2302     @BeanProperty(bound = false)
    2303     public List<E> getSelectedValuesList() {
    2304         ListSelectionModel sm = getSelectionModel();
    2305         ListModel<E> dm = getModel();

    2306
    2307         int iMin = sm.getMinSelectionIndex();
    2308         int iMax = sm.getMaxSelectionIndex();
    2309         int size = dm.getSize();
    2310
    2311         if ((iMin < 0) || (iMax < 0) || (iMin >= size)) {
    2312             return Collections.emptyList();
    2313         }
    2314         iMax = iMax < size ? iMax : size - 1;
    2315
    2316         List<E> selectedItems = new ArrayList<E>();
    2317         for(int i = iMin; i <= iMax; i++) {
    2318             if (sm.isSelectedIndex(i)) {

    2319                 selectedItems.add(dm.getElementAt(i));
    2320             }
    2321         }
    2322         return selectedItems;

    2323     }
    2324
    2325
    2326     /**
    2327      * Returns the smallest selected cell index; <i>the selection</i> when only
    2328      * a single item is selected in the list. When multiple items are selected,
    2329      * it is simply the smallest selected index. Returns {@code -1} if there is
    2330      * no selection.
    2331      * <p>
    2332      * This method is a cover that delegates to {@code getMinSelectionIndex}.
    2333      *
    2334      * @return the smallest selected cell index
    2335      * @see #getMinSelectionIndex
    2336      * @see #addListSelectionListener
    2337      */
    2338     public int getSelectedIndex() {
    2339         return getMinSelectionIndex();
    2340     }
    2341
    2342
    2163      *
    2164      * @see #setValueIsAdjusting
    2165      * @see ListSelectionModel#getValueIsAdjusting
    2166      */
    2167     public boolean getValueIsAdjusting() {
    2168         return getSelectionModel().getValueIsAdjusting();
    2169     }
    2170
    2171
    2172     /**
    2173      * Returns an array of all of the selected indices, in increasing
    2174      * order.
    2175      *
    2176      * @return all of the selected indices, in increasing order,
    2177      *         or an empty array if nothing is selected
    2178      * @see #removeSelectionInterval
    2179      * @see #addListSelectionListener
    2180      */
    2181     @Transient
    2182     public int[] getSelectedIndices() {
    2183         return getSelectionModel().getSelectedIndices();

    2184     }
    2185
    2186
    2187     /**
    2188      * Selects a single cell. Does nothing if the given index is greater
    2189      * than or equal to the model size. This is a convenience method that uses
    2190      * {@code setSelectionInterval} on the selection model. Refer to the
    2191      * documentation for the selection model class being used for details on
    2192      * how values less than {@code 0} are handled.
    2193      *
    2194      * @param index the index of the cell to select
    2195      * @see ListSelectionModel#setSelectionInterval
    2196      * @see #isSelectedIndex
    2197      * @see #addListSelectionListener
    2198      */
    2199     @BeanProperty(bound = false, description
    2200             = "The index of the selected cell.")
    2201     public void setSelectedIndex(int index) {
    2202         if (index >= getModel().getSize()) {
    2203             return;
    2267             }
    2268         }
    2269         Object[] rv = new Object[n];
    2270         System.arraycopy(rvTmp, 0, rv, 0, n);
    2271         return rv;
    2272     }
    2273
    2274     /**
    2275      * Returns a list of all the selected items, in increasing order based
    2276      * on their indices in the list.
    2277      *
    2278      * @return the selected items, or an empty list if nothing is selected
    2279      * @see #isSelectedIndex
    2280      * @see #getModel
    2281      * @see #addListSelectionListener
    2282      *
    2283      * @since 1.7
    2284      */
    2285     @BeanProperty(bound = false)
    2286     public List<E> getSelectedValuesList() {

    2287         ListModel<E> dm = getModel();
    2288         int[] selectedIndices = getSelectedIndices();
    2289
    2290         if (selectedIndices.length > 0) {

    2291             int size = dm.getSize();
    2292             if (selectedIndices[0] >= size) {

    2293                 return Collections.emptyList();
    2294             }

    2295             List<E> selectedItems = new ArrayList<E>();
    2296             for (int i : selectedIndices) {
    2297                 if (i >= size)
    2298                     break;
    2299                 selectedItems.add(dm.getElementAt(i));
    2300             }

    2301             return selectedItems;
    2302         }
    2303         return Collections.emptyList();
    2304     }
    2305
    2306
    2307     /**
    2308      * Returns the smallest selected cell index; <i>the selection</i> when only
    2309      * a single item is selected in the list. When multiple items are selected,
    2310      * it is simply the smallest selected index. Returns {@code -1} if there is
    2311      * no selection.
    2312      * <p>
    2313      * This method is a cover that delegates to {@code getMinSelectionIndex}.
    2314      *
    2315      * @return the smallest selected cell index
    2316      * @see #getMinSelectionIndex
    2317      * @see #addListSelectionListener
    2318      */
    2319     public int getSelectedIndex() {
    2320         return getMinSelectionIndex();
    2321     }
    2322
    2323

< prev index next >