按名称对JList排序

时间:2013-11-26 20:10:36

标签: java swing sorting jlist

我正在做的是从地图中获取元素并将它们添加到JList以显示在GUI上。我想知道如何按字母顺序对名称进行排序。

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

2 个答案:

答案 0 :(得分:0)

算法:

  1. 从JList获取所有值,将它们转换为字符串,存储在数组
  2. 对数组进行排序
  3. 将新值设置为JList。
  4. 代码:

        JList jl = new JList(new Object[]{4.5,1,"Hi!"});
        ListModel model = jl.getModel();
        String[] strings = new String[model.getSize()];
        for(int i=0;i<strings.length;i++){
            strings[i]=model.getElementAt(i).toString();
        }
        Arrays.sort(strings);
        jl.setListData(strings);    
    

    如果您需要按任何其他顺序对数组进行排序,请参阅Comparator

答案 1 :(得分:0)

这是一个小例子,它可以对您的商店名称进行排序。

ShopComparator类执行排序任务:

package model;

import java.util.Comparator;

public class ShopComparator implements Comparator<Shop> {

    @Override
    public int compare(Shop o1, Shop o2) {
        return o1.getName().compareTo(o2.getName());
    }

}

Shop类,尽可能简单:

package model;

public class Shop {

    private int id;
    private String name;

    public Shop(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

主应用程序:

package model;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

public class App {

    public static void main(String[] args) {
        Map<String, Shop> shops = new HashMap<String, Shop>();
        Shop s1 = new Shop(1, "Apus Drugstore");
        Shop s2 = new Shop(2, "DM");
        Shop s3 = new Shop(3, "Kaufhof");
        Shop s4 = new Shop(4, "Moes Traverne");

        shops.put("one", s3);
        shops.put("two", s4);
        shops.put("three", s1);
        shops.put("four", s2);

        for(Shop s : shops.values()) {
            System.out.println(s.getName());
        }
        ShopComparator sc = new ShopComparator();
        TreeSet<Shop> sortedShops = new TreeSet<>(sc);

        sortedShops.addAll(shops.values());

        for(Shop s : sortedShops) {
            System.out.println(s.getName());
        }
    }

}

第一个输出,未排序: Moes Traverne 考夫霍夫 Apus Drugstore DM

和排序的输出。

Apus Drugstore DM 考夫霍夫 Moes Traverne