2342 * list. When multiple items are selected, it is simply the value for the 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 2388 /** 2389 * --- The Scrollable Implementation --- 2390 */ 2391 2392 private void checkScrollableParameters(Rectangle visibleRect, int orientation) { 2393 if (visibleRect == null) { 2394 throw new IllegalArgumentException("visibleRect must be non-null"); 2395 } 2396 switch (orientation) { 2397 case SwingConstants.VERTICAL: 2398 case SwingConstants.HORIZONTAL: 2399 break; 2400 default: 2401 throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL"); | 2342 * list. When multiple items are selected, it is simply the value for the 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 * If the object passed is not present in the list, the selection is 2363 * cleared. 2364 * 2365 * @param anObject the object to select 2366 * @param shouldScroll {@code true} if the list should scroll to display 2367 * the selected object, if one exists; otherwise {@code false} 2368 */ 2369 public void setSelectedValue(Object anObject,boolean shouldScroll) { 2370 E selectedValue = getSelectedValue(); 2371 //This will be true if both anObject and selectedObject are either 2372 // null or point to objects which return true on calling equals 2373 if((anObject == selectedValue) || 2374 (anObject != null && anObject.equals(selectedValue))) { 2375 return; 2376 } else { 2377 int i,c; 2378 ListModel<E> dm = getModel(); 2379 for(i=0,c=dm.getSize();i<c;i++) { 2380 Object object = dm.getElementAt(i); 2381 //Either both should be null or reference to objects which 2382 // return true with equals method 2383 if ((anObject == object) || 2384 anObject!= null && anObject.equals(object)) { 2385 setSelectedIndex(i); 2386 if (shouldScroll) 2387 ensureIndexIsVisible(i); 2388 repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/ 2389 return; 2390 } 2391 } 2392 clearSelection(); 2393 } 2394 repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/ 2395 } 2396 2397 2398 2399 /** 2400 * --- The Scrollable Implementation --- 2401 */ 2402 2403 private void checkScrollableParameters(Rectangle visibleRect, int orientation) { 2404 if (visibleRect == null) { 2405 throw new IllegalArgumentException("visibleRect must be non-null"); 2406 } 2407 switch (orientation) { 2408 case SwingConstants.VERTICAL: 2409 case SwingConstants.HORIZONTAL: 2410 break; 2411 default: 2412 throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL"); |