implementing ListCellRenderer

1.5k views Asked by At

I am stuck with implementing ListCellRenderer. This is my code.

I am getting the data from a DB in the form of Domain class that looks like this:

public class Domain {

    private Integer id;
    private String naziv;
    private Integer status;

    public Domain(){}

    public Integer getId() {return id;}
    public void setId(int i){id = i;}

    public String getNaziv(){return naziv;}
    public void setNaziv(String n){naziv = n;}

    public Integer getStatus(){return status;}
    public void setStatus(int s){status = s;}
}

Set up of JList:

        DefaultListSelectionModel m = new DefaultListSelectionModel();
        m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m.setLeadAnchorNotificationEnabled(false);
        DefaultListModel<String> modelRN = new DefaultListModel<String>();
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 86, 390, 199);
        contentPane.add(scrollPane);
        JList<String> listRN = new JList<String>(modelRN);
        scrollPane.setViewportView(listRN);
        listRN.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        listRN.setSelectionModel(m);

and this is how I populate the list:

        dRN = new DBdomain(s,a,b).Conn();
        for(int i=0;i<dRN.size();i++){
        modelRN.addElement(dRN.get(i).getNaziv()); 

where dRN = ArrayList<Domain>

So the problem is this. I am populating the list with strings that are Domain.getNaziv() but i want to change the background in the list where Domain.getStatus() has certain value. I know I need to implement something like this:

public class MyListCellRenderer extends DefaultListCellRenderer {
         @Override
         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
             Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
             // do something
             return c;
         }
    }

The problem is that I am not populating Jlist with Domain but with a Domain filed which is a string, so the value argument in getListCellRendererComponent doesnt see the filed status so I don't know how I would mark the fields whose background I want to change.

hope I provided all the info and that somebody can point me in the right direction.

1

There are 1 answers

7
martinez314 On BEST ANSWER

The problem is that I am not populating Jlist with Domain

Well DO populate JList with Domain. Perhaps something like this:

public class MyListCellRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Component cell = null;

        if (value instanceof Domain) {
            Domain domain = (Domain)value;
            int status = domain.getStatus();
            String naziv = domain.getNaziv();

            cell = super.getListCellRendererComponent(list, 
                naziv, // note this...
                index, 
                isSelected, 
                cellHasFocus);

            if (status > 0) { // or whatever...
                cell.setBackground(STATUS_ON_COLOR);
            } else {
                cell.setBackground(STATUS_OFF_COLOR);
            }
        }
    }

    return cell;
}