如果它们有值,我正在尝试更改 JTable 第三行的单元格的颜色。我读到这样做的一个好方法是使用表格单元格渲染器。但是,它似乎什么也没做!这是我的渲染代码:
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;
}
}
这里是我如何使用我的渲染器:
tablaUsuariosPrestamos.setDefaultRenderer(Object.class,new RenderTablaPrestamos());
这是我的 JTable 的图片(我认为模型代码没有什么用,因为它有点长):
我不认为它与if clausules有任何关系,因为我评论过它们并且它也不起作用。
我哪里错了?