[英]Java order jlist by status
i have a small problem, i don't know how to sort my jlist by status which is retrieved from database. 我有一个小问题,我不知道如何按照从数据库中检索到的状态对我的jlist进行排序。 i want sort by "online" and "offline", i mean online computers go first and then offline computers, i have this code now, it just makes the icon+text for the jlist
我希望按“在线”和“离线”排序,我的意思是在线计算机首先然后离线计算机,我现在有这个代码,它只是为jlist制作图标+文本
Can you tell me how can i filter/sortby status? 你能告诉我怎么过滤/排序状态?
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);
}
}
I want to do something like (should automatically order) http://imageshack.us/a/img27/9018/2mx1.png 我想做一些事情(应该自动订购) http://imageshack.us/a/img27/9018/2mx1.png
and not: http://imageshack.us/a/img407/346/e9r.png 而不是: http : //imageshack.us/a/img407/346/e9r.png
You can sort your pc_list
using the Collections.sort
utility method a custom Comparator
. 您可以使用
Collections.sort
实用程序方法对自定义Comparator
对pc_list
进行排序。
However, that's probably overkill. 然而,这可能是矫枉过正的。 It would be easier to just use two lists:
使用两个列表会更容易:
ArrayList<String> pc_list = new ArrayList<>();
ArrayList<String> pc_offline_list = new ArrayList<>();
// . . .
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")));
pc_list.add(comb);
break;
case "offline":
//This is just for rendering an image to Jlist
icons.put(comb, new ImageIcon(getClass().getResource("/Imagenes/com_off_30x30.png")));
pc_offline_list.add(comb);
break;
}
// . . .
pc_list.addAll(pc_offline_list);
Home.computer_jlist.setListData(pc_list.toArray());
So you keep the offline ones separate at first, and then tack them all onto the end one you've found them all. 因此,您首先要将离线版本分开,然后将它们全部添加到您已找到它们的所有内容中。 By keeping them separate avoid having to do any sorting!
通过保持它们分开避免必须进行任何排序!
Also, the add
method adds to the end (appends) by default, so you'll notice that I just wrote pc_list.add(comb)
instead of pc_list.add(i, comb)
. 此外,默认情况下,
add
方法会添加到末尾(追加),所以你会注意到我只是写了pc_list.add(comb)
而不是pc_list.add(i, comb)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.