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):
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?
Use
for (int i = 0; i < tabla.getColumnCount(); i++) {
tabla.getColumnModel().getColumn(i).setCellRenderer(new RenderTablaPrestamos());
}
instead of
tablaUsuariosPrestamos.setDefaultRenderer(Object.class,new RenderTablaPrestamos());