swing - Java order jlist by status -
i have small problem, don't know how sort jlist status retrieved database. want sort "online" , "offline", mean online computers go first , offline computers, have code now, makes icon+text jlist
can tell me how can 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 = 0; while (rs.next()) { //getting info db string pc_name = rs.getstring("nombre_pc"); string pc_ip = rs.getstring("ip"); string status = rs.getstring("estado"); //setting text jlist comb = pc_name + " - " + pc_ip; //comparing status switch (status) { case "online": //this rendering image+text jlist icons.put(comb, new imageicon(getclass().getresource("/imagenes/com_on_30x30.png"))); break; case "offline": //this rendering image jlist icons.put(comb, new imageicon(getclass().getresource("/imagenes/com_off_30x30.png"))); break; } //adding info arraylist pc_list.add(i, comb); i++; } con.close(); // setting list/text on jlist home.computer_jlist.setlistdata(pc_list.toarray()); // create cell renderer add appropriate icon home.computer_jlist.setcellrenderer(new pc_cell_render(icons)); } catch (exception e) { system.out.println("error aqui: " + e); } }
i want (should automatically order) http://imageshack.us/a/img27/9018/2mx1.png
you can sort pc_list
using collections.sort
utility method custom comparator
.
however, that's overkill. easier use 2 lists:
arraylist<string> pc_list = new arraylist<>(); arraylist<string> pc_offline_list = new arraylist<>(); // . . . switch (status) { case "online": //this rendering image+text jlist icons.put(comb, new imageicon(getclass().getresource("/imagenes/com_on_30x30.png"))); pc_list.add(comb); break; case "offline": //this rendering image 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 keep offline ones separate @ first, , tack them onto end 1 you've found them all. keeping them separate avoid having sorting!
also, add
method adds end (appends) default, you'll notice wrote pc_list.add(comb)
instead of pc_list.add(i, comb)
.
Comments
Post a Comment