ensure selected list item is always within boundaries

This commit is contained in:
yugecin 2017-03-26 20:00:29 +02:00
parent fb8369e570
commit 9c19b1bddd

View File

@ -93,8 +93,9 @@ public class DropdownMenu<E> extends Component {
* @throws IllegalArgumentException if {@code index} is negative or greater than or equal to size * @throws IllegalArgumentException if {@code index} is negative or greater than or equal to size
*/ */
public void setSelectedIndex(int index) { public void setSelectedIndex(int index) {
if (index < 0 || index >= items.length) if (index < 0 || items.length <= index) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
this.selectedItemIndex = index; this.selectedItemIndex = index;
} }
@ -252,7 +253,7 @@ public class DropdownMenu<E> extends Component {
return; return;
} }
this.expanded = (idx == -1) && !expanded; this.expanded = (idx == -1) && !expanded;
if (idx >= 0 && selectedItemIndex != idx) { if (0 <= idx && idx < items.length && selectedItemIndex != idx) {
this.selectedItemIndex = idx; this.selectedItemIndex = idx;
itemSelected(idx, items[selectedItemIndex]); itemSelected(idx, items[selectedItemIndex]);
} }