Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Sorting Combo Box Items
Hi,
Um : I don't understand - you talk about "sorting combo box items", yet the code you give will present the combo box items in the same order that you present them -> The combo box will be sorted correctly.
However, you say :
But when it is added to the combo box the last item is getting selected as default value.
That's a completely different issue, and has nothing to do with sorting; the reason that the last item is getting selected is because you are programatically selecting it, by calling currency.setValue(currencyView.getName())! Take that call out of the loop, and nothing will be selected (and the user will have to select an item themselves.
Of course, if you want to select the first item, that's also possible.
//Add currencies into the combo box
ArrayList<CurrencyView> ccyView = new ArrayList<CurrencyView>();
ccyView = serviceWrapper .getCurrencyDataList(null);
for (Iterator<? extends CurrencyView> currencyIterator = ccyView .iterator(); currencyIterator.hasNext();) {
currencyView = currencyIterator.next();
currency.addItem(currencyView.getName());
}
// Select first currency (if there is one)
Object id = currencyField.getItemIds().iterator().next();
if (id != null) {
currencyField.setValue(currencyField.getItem(id));
}
Cheers,
Charles.