/** * Removes an item from the combo box at a particular index * * @param index Index of the item to remove * @return Itself * @throws IndexOutOfBoundsException if the index is out of range */ public synchronized ComboBox<V> remoteItem(int index) { items.remove(index); if (index < selectedIndex) { setSelectedIndex(selectedIndex - 1); } else if (index == selectedIndex) { setSelectedIndex(-1); } invalidate(); return this; }
/** * Adds a new item to the combo box, at a specific index * * @param index Index to add the item at * @param item Item to add * @return Itself */ public synchronized ComboBox<V> addItem(int index, V item) { if (item == null) { throw new IllegalArgumentException("Cannot add null elements to a ComboBox"); } items.add(index, item); if (index <= selectedIndex) { setSelectedIndex(selectedIndex + 1); } invalidate(); return this; }
/** * Adds a new item to the combo box, at the end * * @param item Item to add to the combo box * @return Itself */ public synchronized ComboBox<V> addItem(V item) { if (item == null) { throw new IllegalArgumentException("Cannot add null elements to a ComboBox"); } items.add(item); if (selectedIndex == -1 && items.size() == 1) { setSelectedIndex(0); } invalidate(); return this; }
private Result handleEditableCBKeyStroke(KeyStroke keyStroke) { // First check if we are in drop-down focused mode, treat keystrokes a bit differently then if (isDropDownFocused()) { switch (keyStroke.getKeyType()) { case ReverseTab: case ArrowLeft: dropDownFocused = false; textInputPosition = text.length(); return Result.HANDLED; // The rest we can process in the same way as with read-only combo boxes when we are in // drop-down focused mode default: return handleReadOnlyCBKeyStroke(keyStroke); } } switch (keyStroke.getKeyType()) { case Character: text = text.substring(0, textInputPosition) + keyStroke.getCharacter() + text.substring(textInputPosition); textInputPosition++; return Result.HANDLED; case Tab: dropDownFocused = true; return Result.HANDLED; case Backspace: if (textInputPosition > 0) { text = text.substring(0, textInputPosition - 1) + text.substring(textInputPosition); textInputPosition--; } return Result.HANDLED; case Delete: if (textInputPosition < text.length()) { text = text.substring(0, textInputPosition) + text.substring(textInputPosition + 1); } return Result.HANDLED; case ArrowLeft: if (textInputPosition > 0) { textInputPosition--; } else { return Result.MOVE_FOCUS_LEFT; } return Result.HANDLED; case ArrowRight: if (textInputPosition < text.length()) { textInputPosition++; } else { dropDownFocused = true; return Result.HANDLED; } return Result.HANDLED; case ArrowDown: if (selectedIndex < items.size() - 1) { setSelectedIndex(selectedIndex + 1); } return Result.HANDLED; case ArrowUp: if (selectedIndex > 0) { setSelectedIndex(selectedIndex - 1); } return Result.HANDLED; default: } return super.handleKeyStroke(keyStroke); }
/** * Removes all items from the combo box * * @return Itself */ public synchronized ComboBox<V> clearItems() { items.clear(); setSelectedIndex(-1); invalidate(); return this; }