0
我正在做的是从地图获取元素并将它们添加到JList以显示在GUI上。我想知道如何按字母顺序排列名称。按名称排序JList
private void refreshShopsList() {
gameShopsJList.setModel(new javax.swing.AbstractListModel<String>() {
public int getSize() {
return ShopsLoader.getShops().size();
}
public String getElementAt(int i) {
return getShopByIndex(i).getName();
}
});
}
private Shop getShopByIndex(int index) {
Iterator<Entry<String, Shop>> it = ShopsLoader.getShops().entrySet().iterator();
int count = -1;
while(it.hasNext()) {
Entry<String, Shop> entry = it.next();
count++;
if (count == index)
return entry.getValue();
}
return null;
}
/**
* The map of the shops
*/
private static final Map<String, Shop> shops = new HashMap<String, Shop>();
public static Map<String, Shop> getShops() {
return shops;
}
完成的AbstractListModel(空torzo),那么将有可能提出好的建议......,为更好地帮助越早张贴SSCCE,短,可运行,编译,从局部变量或枚举中为Map提供硬编码值 –
mKorbel
只需将'Map'从'HashMap'更改为'TreeMap'就可以完成这项工作。但是,对每个'getElementAt'调用执行线性映射迭代的列表模型的设计都值得怀疑。 – Holger
只有一列使用'JTable'。 'JTable'通过点击标题内置了排序。默认分拣机可以对字符串进行排序 – Robin