on 10-05-2004 9:45 AM
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Francisco,
shaking my head hard to remember this thread, now I'm in again
I think the proposal was straight forward:
> Create an object CountryModelEntry
public class CountryModelEntry {
}
> (consists of key and text),
public class CountryModelEntry {
private String countryKey;
private String countryText;
public CountryModelEntry (String key, String text) {
countryKey = key;
countryText = text;
}
public String getCountryKey() {
return countryKey;
}
public String getCountryText() {
return countryText;
}
}
> implement Comparable
> and define the order by the order of the text.
public class CountryModelEntry implements Comparable {
[...]
public int compareTo(Object o) {
CountryModelEntry compObj = (CountryModelEntry) o;
return this.countryText.compareTo(compObj.countryText);
}
}
> Then create an ArrayList with all CountryModelEntries
if you have to use your countryTable,
go the following way; if not, just fill the entries
where you have filled your table before
ArrayList countryModelEntries = new ArrayList();
for (int i = 0; i < countryTableNumRows; i++){
countryTable.setRow(i);
CountryModelEntry someEntry =
new CountryModelEntry(
countryTable.getString("COUNTRY"),
countryTable.getString("COUNTRYTXT"));
countryModelEntries.add(someEntry);
}
> sort this
Collections.sort(countryModelEntries);
> and create the ListModel in the order
> you get back by your sorted list.
for (int i = 0; i < countryModelEntries.size(); i++) {
CountryModelEntries orderedEntry =
(CountryModelEntry) countryModelEntries.get(i);
countryModel.addItem(
orderedEntry.getCountryKey(),
orderedEntry.getCountryKey());
}
Hope it helps
Detlev
Wow, thanks a lot for your fast and very helpful answer!
Hope your head is not shaking anymore
Kind regards
Francisco
Edit:
Just found a small typo in the last code:
for (int i = 0; i < countryModelEntries.size(); i++) {
CountryModelEntry orderedEntry = (CountryModelEntry) countryModelEntries.get(i);
countryModel.addItem(orderedEntry.getCountryKey(), orderedEntry.getCountryText());
}
Message was edited by: Francisco Villar
User | Count |
---|---|
75 | |
9 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.