< prev index next >

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

Print this page

    rev 48858 : 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.
    3. Reverted the changes made to "getSelectedValues" as suggested by Joe.
    4. Modified files that were failing on Linux and Mac as well.

    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();
    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
    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         return getSelectedValuesList().toArray();

    2268     }
    2269
    2270     /**
    2271      * Returns a list of all the selected items, in increasing order based
    2272      * on their indices in the list.
    2273      *
    2274      * @return the selected items, or an empty list if nothing is selected
    2275      * @see #isSelectedIndex
    2276      * @see #getModel
    2277      * @see #addListSelectionListener
    2278      *
    2279      * @since 1.7
    2280      */
    2281     @BeanProperty(bound = false)
    2282     public List<E> getSelectedValuesList() {
    2283         ListSelectionModel sm = getSelectionModel();
    2284         ListModel<E> dm = getModel();
    2285
    2286         int iMin = sm.getMinSelectionIndex();
    2287         int iMax = sm.getMaxSelectionIndex();
    2324      * smallest selected index. Returns {@code null} if there is no selection.
    2325      * <p>
    2326      * This is a convenience method that simply returns the model value for
    2327      * {@code getMinSelectionIndex}.
    2328      *
    2329      * @return the first selected value
    2330      * @see #getMinSelectionIndex
    2331      * @see #getModel
    2332      * @see #addListSelectionListener
    2333      */
    2334     @BeanProperty(bound = false)
    2335     public E getSelectedValue() {
    2336         int i = getMinSelectionIndex();
    2337         return (i == -1) ? null : getModel().getElementAt(i);
    2338     }
    2339
    2340
    2341     /**
    2342      * Selects the specified object from the list.
    2343      *
    2344      * @param <E> the type of the elements of this JList.
    2345      * @param anObject      the object to select
    2346      * @param shouldScroll  {@code true} if the list should scroll to display
    2347      *                      the selected object, if one exists; otherwise {@code false}
    2348      */
    2349     public void setSelectedValue(E anObject,boolean shouldScroll) {
    2350         if(anObject == null)
    2351             setSelectedIndex(-1);
    2352         else if(!anObject.equals(getSelectedValue())) {
    2353             int i,c;
    2354             ListModel<E> dm = getModel();
    2355             for(i=0,c=dm.getSize();i<c;i++)
    2356                 if(anObject.equals(dm.getElementAt(i))){
    2357                     setSelectedIndex(i);
    2358                     if(shouldScroll)
    2359                         ensureIndexIsVisible(i);
    2360                     repaint();  /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
    2361                     return;
    2362                 }
    2363             setSelectedIndex(-1);
    2364         }
    2365         repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
    2366     }
    2367
    2368
    2369

< prev index next >