JComboBox – Items


Add Item

jComboBox1.addItem("New Item");

Add Collection of Items

ArrayList list = new ArrayList<>();
list.add("New Item Collection (1 of 3)");
list.add("New Item Collection (2 of 3)");
list.add("New Item Collection (3 of 3)");
((DefaultComboBoxModel) jComboBox1.getModel()).addAll(list);

Insert Item

int index = jComboBox1.getSelectedIndex();
if (index >= 0) {
    jComboBox1.insertItemAt("Inserted Item", index);
}
Note:
    index must be >= 0 and <= ItemCount

Replace Item

int index = jComboBox1.getSelectedIndex();
if (index >= 0) {
    jComboBox1.removeItemAt(index);
    jComboBox1.insertItemAt("Replaced Item", index);
}

Remove Item

int index = jComboBox1.getSelectedIndex();
if (index >= 0) {
    jComboBox1.removeItemAt(index);
}

Note:
    index must be >= 0 and < ItemCount

Remove All Items

jComboBox1.removeAllItems();

Item Count

System.out.println("Item Count: "+jComboBox1.getItemCount());