How do I print all the items within a JComboBox?

user1927670 picture user1927670 · Dec 25, 2012 · Viewed 21.2k times · Source

I'm wondering how to print out ALL the items within a JComboBox. I have no idea how to go about doing this. I know how to print out whatever item is selected. I just need it to where when I press a button, it prints out every option in the JComboBox.

Answer

Vimm picture Vimm · Nov 23, 2016

I know it's an old question, but I found it easier to skip the ComboBoxModel.

String items = new String[]{"Rock", "Paper", "Scissors"};
JComboBox<String> comboBox = new JComboBox<>(items);

int size = comboBox.getItemCount();
for (int i = 0; i < size; i++) {
  String item = comboBox.getItemAt(i);
  System.out.println("Item at " + i + " = " + item);
}