0
我想改变我的JTable的第三行的单元格的颜色,如果他们有一个值。我读过这样做的好方法是使用表格单元格渲染器。但是,它似乎没有做任何事情!在这里有我的渲染代码:DefaultTableCellRenderer不起作用
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的照片(我不认为模型代码将是任何有用的因为它是有点长):
我不认为它做任何事情做的如果clausules,一我评论他们,也没有效果。
我哪里错了?
你设置Object类的渲染器。您的模型似乎表明第三个olumn的类是Integer(因为它们已经以特定方式呈现)。举办一个完整的最小程序再现问题。无需发布您的真实模型。此外,您至少可以简单地通过使用调试器并设置断点来检查您的渲染器是否被调用。你为什么不那样做? –
为了更好地提供帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。顺便说一句 - 标签需要是不透明的才能显示BG颜色。 –