< prev index next >

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

Print this page

    rev 48358 : Work in progress for JDK-6303622
    Use generic in Swing components like JComboBox
    1. Added generic return type and parameter type for JComboBox.
    2. Modified JList related classes as well.

    2237      * @see #isSelectedIndex
    2238      * @see #addListSelectionListener
    2239      * @throws NullPointerException if the given array is {@code null}
    2240      */
    2241     public void setSelectedIndices(int[] indices) {
    2242         ListSelectionModel sm = getSelectionModel();
    2243         sm.clearSelection();
    2244         int size = getModel().getSize();
    2245         for (int i : indices) {
    2246             if (i < size) {
    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
    2343      * smallest selected index. Returns {@code null} if there is no selection.
    2344      * <p>
    2345      * This is a convenience method that simply returns the model value for
    2346      * {@code getMinSelectionIndex}.
    2347      *
    2348      * @return the first selected value
    2349      * @see #getMinSelectionIndex
    2350      * @see #getModel
    2351      * @see #addListSelectionListener
    2352      */
    2353     @BeanProperty(bound = false)
    2354     public E getSelectedValue() {
    2355         int i = getMinSelectionIndex();
    2356         return (i == -1) ? null : getModel().getElementAt(i);
    2357     }
    2358
    2359
    2360     /**
    2361      * Selects the specified object from the list.
    2362      *

    2363      * @param anObject      the object to select
    2364      * @param shouldScroll  {@code true} if the list should scroll to display
    2365      *                      the selected object, if one exists; otherwise {@code false}
    2366      */
    2367     public void setSelectedValue(Object anObject,boolean shouldScroll) {
    2368         if(anObject == null)
    2369             setSelectedIndex(-1);
    2370         else if(!anObject.equals(getSelectedValue())) {
    2371             int i,c;
    2372             ListModel<E> dm = getModel();
    2373             for(i=0,c=dm.getSize();i<c;i++)
    2374                 if(anObject.equals(dm.getElementAt(i))){
    2375                     setSelectedIndex(i);
    2376                     if(shouldScroll)
    2377                         ensureIndexIsVisible(i);
    2378                     repaint();  /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
    2379                     return;
    2380                 }
    2381             setSelectedIndex(-1);
    2382         }
    2383         repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
    2384     }
    2385
    2386
    2387
    2237      * @see #isSelectedIndex
    2238      * @see #addListSelectionListener
    2239      * @throws NullPointerException if the given array is {@code null}
    2240      */
    2241     public void setSelectedIndices(int[] indices) {
    2242         ListSelectionModel sm = getSelectionModel();
    2243         sm.clearSelection();
    2244         int size = getModel().getSize();
    2245         for (int i : indices) {
    2246             if (i < size) {
    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      * @param <E> the type of the elements of this JList.
    2258      * @return the selected values, or an empty array if nothing is selected
    2259      * @see #isSelectedIndex
    2260      * @see #getModel
    2261      * @see #addListSelectionListener
    2262      *
    2263      * @deprecated As of JDK 1.7, replaced by {@link #getSelectedValuesList()}
    2264      */
    2265     @Deprecated
    2266     @BeanProperty(bound = false)
    2267     public E[] getSelectedValues() {
    2268         ListSelectionModel sm = getSelectionModel();
    2269         ListModel<E> dm = getModel();
    2270
    2271         int iMin = sm.getMinSelectionIndex();
    2272         int iMax = sm.getMaxSelectionIndex();
    2273
    2274         if ((iMin < 0) || (iMax < 0)) {
    2275             @SuppressWarnings("unchecked")
    2276             E[] res = (E[])new Object[0];
    2277             return res;
    2278         }
    2279
    2280         @SuppressWarnings("unchecked")
    2281         E[] rvTmp = (E[]) new Object[1+ (iMax - iMin)];
    2282         int n = 0;
    2283         for(int i = iMin; i <= iMax; i++) {
    2284             if (sm.isSelectedIndex(i)) {
    2285                 rvTmp[n++] = dm.getElementAt(i);
    2286             }
    2287         }
    2288         @SuppressWarnings("unchecked")
    2289         E[] rv = (E[]) new Object[n];
    2290         System.arraycopy(rvTmp, 0, rv, 0, n);
    2291         return rv;
    2292     }
    2293
    2294     /**
    2295      * Returns a list of all the selected items, in increasing order based
    2296      * on their indices in the list.
    2297      *
    2298      * @return the selected items, or an empty list if nothing is selected
    2299      * @see #isSelectedIndex
    2300      * @see #getModel
    2301      * @see #addListSelectionListener
    2302      *
    2303      * @since 1.7
    2304      */
    2305     @BeanProperty(bound = false)
    2306     public List<E> getSelectedValuesList() {
    2307         ListSelectionModel sm = getSelectionModel();
    2308         ListModel<E> dm = getModel();
    2309
    2348      * smallest selected index. Returns {@code null} if there is no selection.
    2349      * <p>
    2350      * This is a convenience method that simply returns the model value for
    2351      * {@code getMinSelectionIndex}.
    2352      *
    2353      * @return the first selected value
    2354      * @see #getMinSelectionIndex
    2355      * @see #getModel
    2356      * @see #addListSelectionListener
    2357      */
    2358     @BeanProperty(bound = false)
    2359     public E getSelectedValue() {
    2360         int i = getMinSelectionIndex();
    2361         return (i == -1) ? null : getModel().getElementAt(i);
    2362     }
    2363
    2364
    2365     /**
    2366      * Selects the specified object from the list.
    2367      *
    2368      * @param <E> the type of the elements of this JList.
    2369      * @param anObject      the object to select
    2370      * @param shouldScroll  {@code true} if the list should scroll to display
    2371      *                      the selected object, if one exists; otherwise {@code false}
    2372      */
    2373     public void setSelectedValue(E anObject,boolean shouldScroll) {
    2374         if(anObject == null)
    2375             setSelectedIndex(-1);
    2376         else if(!anObject.equals(getSelectedValue())) {
    2377             int i,c;
    2378             ListModel<E> dm = getModel();
    2379             for(i=0,c=dm.getSize();i<c;i++)
    2380                 if(anObject.equals(dm.getElementAt(i))){
    2381                     setSelectedIndex(i);
    2382                     if(shouldScroll)
    2383                         ensureIndexIsVisible(i);
    2384                     repaint();  /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
    2385                     return;
    2386                 }
    2387             setSelectedIndex(-1);
    2388         }
    2389         repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
    2390     }
    2391
    2392
    2393

< prev index next >