Hi all,
I have a HTMLB dropdownListBox which is filled with a DefaultListModel showing a country selection. The listbox is sorted by the countrycode (AD,AE,AF,AG,AL,AM ...)
, but I want it to be sorted by the country text (Andorra,
United Arab Emirates,Afghanistan,Antigua/Barbuda,Albania ...).
How can this be done?
Kind regards,
Francisco
This is the code which fills the model:
for (int i = 0; i < countryTableNumRows; i++) { countryTable.setRow(i); countryModel.addItem(countryTable.getString("COUNTRY"), countryTable.getString("COUNTRYTXT")); }
Hi Francisco,
the keys and texts within the DefaultListModel are backed up by ArrayLists. So the order of the entries is determined by the order in which the keys/texts are added to the model.
You say "listbox is sorted by the countrycode" - the sort order in your example comes from the order the entries are found within the table.
Just as a proposal: Create an object CountryModelEntry (consists of key and text), implement Comparable and define the order by the order of the text. Then create an ArrayList with all CountryModelEntries, sort this and create the ListModel in the order you get back by your sorted list.
Hope it helps
Detlev
Add a comment