2013-11-26 124 views
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; 
} 
+0

完成的AbstractListModel(空torzo),那麼將有可能提出好的建議......,爲更好地幫助越早張貼SSCCE,短,可運行,編譯,從局部變量或枚舉中爲Map 提供硬編碼值 – mKorbel

+0

只需將'Map'從'HashMap'更改爲'TreeMap'就可以完成這項工作。但是,對每個'getElementAt'調用執行線性映射迭代的列表模型的設計都值得懷疑。 – Holger

+1

只有一列使用'JTable'。 'JTable'通過點擊標題內置了排序。默認分揀機可以對字符串進行排序 – Robin

回答

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

} 

的店鋪等級,儘可能簡單:

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 Kaufhof Apus藥店 DM

和排序的輸出。

阿普斯藥店 DM 考夫霍夫 教育和體育部Traverne

0

算法:

  1. 獲得的所有值從JList中,他們
  2. 設置新值轉換爲字符串,存放於數組
  3. 排序數組的JList。

代碼:

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如果您需要任何其他順序進行排序陣列。