Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按名称对JList排序_Java_Swing_Sorting_Jlist - Fatal编程技术网

Java 按名称对JList排序

Java 按名称对JList排序,java,swing,sorting,jlist,Java,Swing,Sorting,Jlist,我所做的是从地图中获取元素,并将它们添加到JList中以在GUI上显示。我想知道如何按字母顺序排列这些名字 private void refreshShopsList() { gameShopsJList.setModel(new javax.swing.AbstractListModel<String>() { public int getSize() { return ShopsLoader.getShops().size();

我所做的是从地图中获取元素,并将它们添加到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;
}
private void refreshShopsList(){
setModel(新的javax.swing.AbstractListModel(){
公共int getSize(){
return ShopsLoader.getShops().size();
}
公共字符串getElementAt(int i){
返回getShopByIndex(i).getName();
}
});     
}
私人店铺getShopByIndex(整数索引){
迭代器it=ShopsLoader.getShops().entrySet().Iterator();
整数计数=-1;
while(it.hasNext()){
Entry=it.next();
计数++;
如果(计数==索引)
返回条目.getValue();
}
返回null;
}
/**
*商店地图
*/
私有静态最终映射商店=新HashMap();
公共静态映射getShops(){
退货店;
}
算法:

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

    jlistjl=newjlist(新对象[]{4.5,1,“Hi!”});
    ListModel model=jl.getModel();
    String[]strings=新字符串[model.getSize()];
    
    对于(inti=0;i,这里有一个小示例,它对您的店铺名称进行排序

    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());
        }
    
    }
    
    和主应用程序:

    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());
            }
        }
    
    }
    
    包模型;
    导入java.util.HashMap;
    导入java.util.Map;
    导入java.util.TreeMap;
    导入java.util.TreeSet;
    公共类应用程序{
    公共静态void main(字符串[]args){
    Map shops=newhashmap();
    商店s1=新商店(1,“Apus药店”);
    车间s2=新车间(2,“DM”);
    商店s3=新商店(3,“考夫霍夫”);
    车间s4=新车间(4,“Moes Traverne”);
    铺位("一",s3);;
    (二字),中四;;
    (三),s1 ;;
    铺位; put(四),s2 ;;
    对于(Shop s:shops.values()){
    System.out.println(s.getName());
    }
    ShopComparator sc=新的ShopComparator();
    TreeSet-sortedShops=新的TreeSet(sc);
    sortedShops.addAll(shops.values());
    用于(车间:分拣车间){
    System.out.println(s.getName());
    }
    }
    }
    
    第一个输出,未排序: 莫斯·特拉凡纳 百货商店 Apus药店 DM

    和排序后的输出

    Apus药店 DM 百货商店
    Moes Traverne

    完成您的AbstractListModel(空torzo),然后将有可能提出一些建议….为了更好的帮助,请尽快发布一个SSCCE,简短、可运行、可编译,并将Map的值从局部变量或枚举硬编码为Map,只需将
    Map
    HashMap
    更改为
    TreeMap
    非常
    getElementAt
    调用有问题。使用只有一列的
    JTable
    JTable
    通过单击标题具有内置排序功能。默认排序器可以对
    字符串进行排序
    
    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());
            }
        }
    
    }