1

I am trying to change the color of the cells of the third row of my JTable if they have a value. I read that a good way to do this is using a table cell renderer. However, it just seems not to be doing anything! Here there is my RENDERER code:

public class RenderTablaPrestamos extends DefaultTableCellRenderer{

   @Override
   public Component getTableCellRendererComponent (JTable tabla, Object valor,
                                                boolean isSelected, boolean hasFocus,
                                                int row, int col){
    JLabel celda = (JLabel) super.getTableCellRendererComponent(tabla, valor, isSelected, hasFocus, row, col);

        if(valor instanceof Integer){
            Integer v=(Integer)valor;
            if(col==3){
                if(valor!=null){
                     celda.setBackground(Color.red);
                }
                else{
                    celda.setBackground(Color.WHITE);
                }
            }
            else{
                celda.setBackground(Color.WHITE);
            }
        }
    return celda;
    }
}

Here there is how I use my renderer:

tablaUsuariosPrestamos.setDefaultRenderer(Object.class,new RenderTablaPrestamos());

Here there is a picture of my JTable (I do not think the model code would be any useful as it is kinda long):

enter image description here

I do not think it does anything to do with the if clausules, as I commented them and it did not work either.

Where am I going wrong?

  • You're setting the renderer for the Object class. Your model seems to indicate that the class for th third olumn is Integer (since they're rendered in a specific way already). Pos a complete minimal program reproducing th problem. No need to post your real model. Also, You could at least simply check if your renderer is called by using your debugger and setting a breakpoint. Why don't you do that? – JB Nizet Feb 26 '17 at 16:42
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). BTW - a label needs to be opaque in order to show a BG color. – Andrew Thompson Feb 26 '17 at 17:23

2 Answers2

3

Use

for (int i = 0; i < tabla.getColumnCount(); i++) {
    tabla.getColumnModel().getColumn(i).setCellRenderer(new RenderTablaPrestamos());
}

instead of

tablaUsuariosPrestamos.setDefaultRenderer(Object.class,new RenderTablaPrestamos());
1

The code in setDefaultRenderer functions as intended:

final YourCellRenderer cellRenderer = new YourCellRenderer();
YourTableModel stModel = new YourTableModel();
table = new JTable(stModel);

table.setDefaultRenderer(YourComponent.class, cellRenderer);

The reason that it doesn't appear to render is because while you are mapping the YourComponent.class to the renderer, it's not firing because the YourTableModel thinks the class is an Object.

To correct this, you need to override the getColumnClass method within the YourTableModel class:

public class YourTableModel extends AbstractTableModel {

    ...

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return YourComponent.class;
    }
    ...
}
Richard
  • 386
  • 4
  • 8