Java order jlist by status

Posted by Takami on Stack Overflow See other posts from Stack Overflow or by Takami
Published on 2013-06-29T04:14:05Z Indexed on 2013/06/29 4:21 UTC
Read the original article Hit count: 153

Filed under:

i have a small problem, i don't know how to sort my jlist by status which is retrieved from database. 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

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

and not: http://imageshack.us/a/img407/346/e9r.png

© Stack Overflow or respective owner

Related posts about java