Johanna
Johanna

Reputation: 27640

How can I get text from ComboBox?

I want to get the items of a combobox, and store it in an ArrayList object.

Upvotes: 1

Views: 8984

Answers (2)

Andreas Dolk
Andreas Dolk

Reputation: 114777

If you just need the selected items (most typical usecase), then just do

Object[] allSelectedAsArray = combobox.getSelectedObjects();
List<Object> allSelectedAsList = Arrays.asList(allSelectedAsArray);

Otherwise (maybe someone added a value to the combobox on the UI)

List<Object> allItemsAsList = new ArrayList<Object>();
for (int index = 0; index < combobox.getItemCount(); index++) {
  Object item = combobox.getItemAt(index);
  allItemsAsList.add(item);
}

Upvotes: 1

TStamper
TStamper

Reputation: 30364

I would suggest using the getItemCount to find out how many items are in the combobox then make a for loop using JComboBox's getItemAt to store the items in your created Arraylist using the Arraylist add()

Upvotes: 1

Related Questions