1

这是我的ListCellRenderer

public class MyListCellRendererOTP 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();
                int naziv1= domain.getId();
                String naziv2=naziv+"  "+naziv1;
                ArrayList<Stavka> s=domain.getStavke();
                int nasao=0;
                for(int i=0;i<s.size();i++){
                    if(s.get(i).getTrazKol()!=s.get(i).getIzdKol()){nasao=1;}
                }

                cell = super.getListCellRendererComponent(list,naziv2,index, isSelected, cellHasFocus);
                if(nasao==1){
                    cell.setBackground(Color.cyan);
                }else{
                    cell.setBackground(Color.white);
                }
                if(isSelected){
                    cell.setBackground(Color.green);
                }
            }
        return cell;
    }
  }

我有一个Jlist填充了域。这个想法是让渲染器更改Domain类的某些字段不同的行的背景,如代码所示。但这由于某种原因不起作用。每一行都会受到影响。任何人都可以帮忙吗?

4

0 回答 0