Coloring your JList with custom cell renderer

39 views Asked by At

I have been looking through plenty of examples and I still can't make my JList color each individual elements based on booleans. This is the code that runs when the JFrame with the list opens:

    public RegistroContrato(Sistema unSistema) {
        initComponents();
        sistema = unSistema;
        sistema.addObserver(this);
        //Cargamos las listas
        jListClientes.setListData(sistema.getListaClientes().toArray());
        jListEmpleados.setListData(sistema.getListaEmpleados().toArray());
        jListDepositos.setListData(sistema.getListaDepositos().toArray());
        jListDepositos.setCellRenderer(new ColorRenderer());
    }

My "sistema" or system has all the data stored, when the data for the list is set, I want the progam to color each element of the list based on value of some booleans named "estantes" and "refrigerado". This is the code in the ColorRenderer class:

    class ColorRenderer extends DefaultListCellRenderer {

        public Component getListCellRendererComponent(JList lista, Deposito value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(lista, value, index, isSelected, cellHasFocus);
            //Tiene refrigerado y estantes
            if (value.getEstantes() && value.getRefrigerado()) {
                setBackground(Color.green);
            } //Tiene solo estantes
            else if (value.getEstantes()) {
                setBackground(Color.orange);
            } //Tiene solo refrigerado
            else if (value.getRefrigerado()) {
                setBackground(Color.cyan);
            } //no tiene ninguno
            else {
                setBackground(Color.yellow);
            }
            return this;
        }
    }
0

There are 0 answers