0
我有一個小問題,我不知道如何按照從數據庫檢索到的狀態對我的jlist進行排序。我想通過「在線」和「離線」排序,我的意思是在線電腦先走後離線電腦,我現在有這個代碼,它只是使圖標+文本爲jlistJava訂單狀態爲jlist
你能告訴我我怎麼能過濾/排序狀態?
public void acx_pc(String query) {
try {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
String comb;
Map<Object, Icon> icons = new HashMap<>();
ArrayList<String> pc_list = new ArrayList<>();
int i = 0;
while (rs.next()) {
//Getting info from DB
String pc_name = rs.getString("nombre_pc");
String pc_ip = rs.getString("IP");
String status = rs.getString("estado");
//Setting text for the jList
comb = pc_name + " - " + pc_ip;
//Comparing Status
switch (status) {
case "online":
//This is just for rendering an image+text to Jlist
icons.put(comb, new ImageIcon(getClass().getResource("/Imagenes/com_on_30x30.png")));
break;
case "offline":
//This is just for rendering an image to Jlist
icons.put(comb, new ImageIcon(getClass().getResource("/Imagenes/com_off_30x30.png")));
break;
}
//Adding info to ArrayList
pc_list.add(i, comb);
i++;
}
con.close();
// Setting the list/text on Jlist
Home.computer_jlist.setListData(pc_list.toArray());
// create a cell renderer to add the appropriate icon
Home.computer_jlist.setCellRenderer(new pc_cell_render(icons));
} catch (Exception e) {
System.out.println("Error aqui: " + e);
}
}
我想要做類似的信息(應自動訂購) http://imageshack.us/a/img27/9018/2mx1.png
,而不是: http://imageshack.us/a/img407/346/e9r.png
如果你確實想讓列表按照狀態排序,然後按字母順序排列,你可以在添加它們之前單獨對兩個列表進行排序'Collections.sort(list)' –
@JavaDevil - 這是真的!這絕對是一個好主意。 +1 – IgnisErus
非常感謝! :D多數民衆贊成在幫助:) – Takami