Java Utililty Methods JComboBox Item

List of utility methods to do JComboBox Item

Description

The list of methods to do JComboBox Item are organized into topic(s).

Method

voidaddFirstItem(JComboBox combobox, T item)
Checks if the item is in the combobox.
int idx = -1;
DefaultComboBoxModel<T> model = (DefaultComboBoxModel<T>) combobox.getModel();
for (int i = 0; i < model.getSize(); i++) {
    T t = model.getElementAt(i);
    if (Objects.equals(t, item)) {
        idx = i;
        break;
model.insertElementAt(item, 0);
if (idx >= 0) {
    model.removeElementAt(idx + 1);
voidaddHeadersaveFileTypeItemsTo(JComboBox combo)
add Headersave File Type Items To
for (String s : headersaveFileTypeItems) {
    combo.addItem(s);
voidaddItems(JComboBox comboBox, Iterable items)
add Items
for (Object item : items) {
    comboBox.addItem(item);
booleancontains(final JComboBox comboBox, final Object item)
contains
final Set<Object> items = new HashSet<>();
for (int i = 0; i < comboBox.getItemCount(); i++) {
    items.add(comboBox.getItemAt(i));
return items.contains(item);
voidfillCombo(JComboBox comboBox, Collection items)
fill Combo
comboBox.setModel(new DefaultComboBoxModel(items.toArray()));
intfindComboBoxItemForString(final JComboBox box, final String searchString)
DOCUMENT ME!
if ((box != null) && (searchString != null)) {
    final ComboBoxModel model = box.getModel();
    if (model != null) {
        for (int i = model.getSize(); --i >= 0;) {
            if (searchString.equals(String.valueOf(model.getElementAt(i)))) {
                return i;
return -1;
intgetDataIndexFromComboBox(JComboBox cbb, String dta)
get Data Index From Combo Box
for (int i = 0; i < ((DefaultComboBoxModel) cbb.getModel()).getSize(); i++) {
    String data = (String) ((DefaultComboBoxModel) cbb.getModel()).getElementAt(i);
    if (data.equals(dta))
        return i;
return -1;
ListgetItems(JComboBox box)
return the list of items held by the combo box.
List l = new ArrayList();
for (int i = 0; i < box.getItemCount(); i++) {
    l.add(box.getItemAt(i));
return l;
ListgetItems(JComboBox comboBox)
get Items
List result = new ArrayList();
for (int i = 0; i < comboBox.getItemCount(); i++) {
    Object element = comboBox.getItemAt(i);
    result.add(element);
return result;
voidinsertIntoCombo(JComboBox combo, Object item)
Insert the given item into the combo box, and set it as first selected item.
MutableComboBoxModel model = (MutableComboBoxModel) combo.getModel();
if (model.getSize() == 0) {
    model.insertElementAt(item, 0);
    return;
Object o = model.getElementAt(0);
if (o.equals(item)) {
    return;
...