Skip to content

Commit

Permalink
Merge pull request mabe02#343 from BrunoEberhard/ComboBox_getSeletect…
Browse files Browse the repository at this point in the history
…Item

getSelectedItem should not throw IndexOutOfBoundsException
  • Loading branch information
mabe02 authored Dec 8, 2017
2 parents 07d4c0a + 05545ed commit ed6d5d7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/googlecode/lanterna/gui2/ComboBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@
*/
package com.googlecode.lanterna.gui2;

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.graphics.Theme;
import com.googlecode.lanterna.graphics.ThemeDefinition;
import com.googlecode.lanterna.input.KeyStroke;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.googlecode.lanterna.Symbols;
import com.googlecode.lanterna.TerminalPosition;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.TerminalTextUtils;
import com.googlecode.lanterna.graphics.Theme;
import com.googlecode.lanterna.graphics.ThemeDefinition;
import com.googlecode.lanterna.input.KeyStroke;

/**
* This is a simple combo box implementation that allows the user to select one out of multiple items through a
* drop-down menu. If the combo box is not in read-only mode, the user can also enter free text in the combo box, much
Expand Down Expand Up @@ -374,7 +377,7 @@ private void updateText(String newText) {
}

/**
* Returns the index of the currently selected item
* Returns the index of the currently selected item or -1 for no selection
* @return Index of the currently selected item
*/
public int getSelectedIndex() {
Expand All @@ -384,12 +387,12 @@ public int getSelectedIndex() {
/**
* Returns the item at the selected index, this is the same as calling:
* <pre>
* comboBox.getItem(comboBox.getSelectedIndex());
* getSelectedIndex() > -1 ? getItem(getSelectedIndex()) : null
* </pre>
* @return The item at the selected index
*/
public synchronized V getSelectedItem() {
return getItem(getSelectedIndex());
return getSelectedIndex() > -1 ? getItem(getSelectedIndex()) : null;
}

/**
Expand Down

0 comments on commit ed6d5d7

Please sign in to comment.