< 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;
    2247                 sm.addSelectionInterval(i, i);
    2248             }
    2249         }
    2250     }
    2251
    2252
    2253     /**
    2254      * Returns an array of all the selected values, in increasing order based
    2255      * on their indices in the list.
    2256      *
    2257      * @return the selected values, or an empty array if nothing is selected
    2258      * @see #isSelectedIndex
    2259      * @see #getModel
    2260      * @see #addListSelectionListener
    2261      *
    2262      * @deprecated As of JDK 1.7, replaced by {@link #getSelectedValuesList()}
    2263      */
    2264     @Deprecated
    2265     @BeanProperty(bound = false)
    2266     public Object[] getSelectedValues() {
    2267         ListSelectionModel sm = getSelectionModel();
    2268         ListModel<E> dm = getModel();

    2269
    2270         int iMin = sm.getMinSelectionIndex();
    2271         int iMax = sm.getMaxSelectionIndex();
    2272
    2273         if ((iMin < 0) || (iMax < 0)) {
    2274             return new Object[0];
    2275         }
    2276
    2277         Object[] rvTmp = new Object[1+ (iMax - iMin)];
    2278         int n = 0;
    2279         for(int i = iMin; i <= iMax; i++) {
    2280             if (sm.isSelectedIndex(i)) {
    2281                 rvTmp[n++] = dm.getElementAt(i);
    2282             }
    2283         }
    2284         Object[] rv = new Object[n];
    2285         System.arraycopy(rvTmp, 0, rv, 0, n);
    2286         return rv;
    2287     }

    2288
    2289     /**
    2290      * Returns a list of all the selected items, in increasing order based
    2291      * on their indices in the list.
    2292      *
    2293      * @return the selected items, or an empty list if nothing is selected
    2294      * @see #isSelectedIndex
    2295      * @see #getModel
    2296      * @see #addListSelectionListener
    2297      *
    2298      * @since 1.7
    2299      */
    2300     @BeanProperty(bound = false)
    2301     public List<E> getSelectedValuesList() {
    2302         ListSelectionModel sm = getSelectionModel();
    2303         ListModel<E> dm = getModel();

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

    2319     }
    2320
    2321
    2322     /**
    2323      * Returns the smallest selected cell index; <i>the selection</i> when only
    2324      * a single item is selected in the list. When multiple items are selected,
    2325      * it is simply the smallest selected index. Returns {@code -1} if there is
    2326      * no selection.
    2327      * <p>
    2328      * This method is a cover that delegates to {@code getMinSelectionIndex}.
    2329      *
    2330      * @return the smallest selected cell index
    2331      * @see #getMinSelectionIndex
    2332      * @see #addListSelectionListener
    2333      */
    2334     public int getSelectedIndex() {
    2335         return getMinSelectionIndex();
    2336     }
    2337
    2338
    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;
    2230                 sm.addSelectionInterval(i, i);
    2231             }
    2232         }
    2233     }
    2234
    2235
    2236     /**
    2237      * Returns an array of all the selected values, in increasing order based
    2238      * on their indices in the list.
    2239      *
    2240      * @return the selected values, or an empty array if nothing is selected
    2241      * @see #isSelectedIndex
    2242      * @see #getModel
    2243      * @see #addListSelectionListener
    2244      *
    2245      * @deprecated As of JDK 1.7, replaced by {@link #getSelectedValuesList()}
    2246      */
    2247     @Deprecated
    2248     @BeanProperty(bound = false)
    2249     public Object[] getSelectedValues() {

    2250         ListModel<E> dm = getModel();
    2251         int[] selectedIndices = getSelectedIndices();
    2252         int length = selectedIndices.length;
    2253
    2254         if (length > 0) {
    2255             Object[] rv = new Object[length];
    2256             for (int i = 0; i < length; i++) {
    2257                     rv[i] = dm.getElementAt(selectedIndices[i]);

    2258             }

    2259             return rv;
    2260         }
    2261         return new Object[0];
    2262     }
    2263
    2264     /**
    2265      * Returns a list of all the selected items, in increasing order based
    2266      * on their indices in the list.
    2267      *
    2268      * @return the selected items, or an empty list if nothing is selected
    2269      * @see #isSelectedIndex
    2270      * @see #getModel
    2271      * @see #addListSelectionListener
    2272      *
    2273      * @since 1.7
    2274      */
    2275     @BeanProperty(bound = false)
    2276     public List<E> getSelectedValuesList() {

    2277         ListModel<E> dm = getModel();
    2278         int[] selectedIndices = getSelectedIndices();
    2279
    2280         if (selectedIndices.length > 0) {

    2281             List<E> selectedItems = new ArrayList<E>();
    2282             for (int i : selectedIndices) {

    2283                     selectedItems.add(dm.getElementAt(i));
    2284             }

    2285             return selectedItems;
    2286         }
    2287         return Collections.emptyList();
    2288     }
    2289
    2290
    2291     /**
    2292      * Returns the smallest selected cell index; <i>the selection</i> when only
    2293      * a single item is selected in the list. When multiple items are selected,
    2294      * it is simply the smallest selected index. Returns {@code -1} if there is
    2295      * no selection.
    2296      * <p>
    2297      * This method is a cover that delegates to {@code getMinSelectionIndex}.
    2298      *
    2299      * @return the smallest selected cell index
    2300      * @see #getMinSelectionIndex
    2301      * @see #addListSelectionListener
    2302      */
    2303     public int getSelectedIndex() {
    2304         return getMinSelectionIndex();
    2305     }
    2306
    2307

< prev index next >