cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting a DefaultListModel

Former Member
0 Kudos

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"));
}

Accepted Solutions (1)

Accepted Solutions (1)

detlev_beutner
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hi Detlev,

thanks for your answer. I will try it this way.

Kind regards

Francisco

Former Member
0 Kudos

Hi Detlev,

unfortunately I did not have time to test this yet. Could you please explain your proposal a little more in detail. This would be great.

Kind regards

Francisco

detlev_beutner
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

Answers (0)