改变JTable中特定单元格的颜色

0 java swing jtable colors cell

我正在尝试更改JTable中列中一个或多个特定单元格的颜色.我在这里看到的答案都是指这种特殊的方法;

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
    Component y = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    y.setBackground(new Color(255,0,0));

    return y;
}
Run Code Online (Sandbox Code Playgroud)

但问题是我不知道它是如何工作的,在我的其他类我有一个字符串的Jtable我想根据它们的字符串值改变某些单元格的颜色,但是我发现的解决方案只允许我更改jtable中整列单元格的颜色,而不是特定的单元格.

cam*_*ckr 5

我有一个Jtable字符串,我想根据字符串值更改某些单元格的颜色

所有单元格都使用相同的渲染器,因此您需要每次都重置背景.

您需要在自定义渲染器代码中使用if条件.就像是:

if (!isSelected)
    if (value.equals(...))
        y.setBackground(new Color(255,0,0));
    else
        y.setBackground(table.getBackground())
Run Code Online (Sandbox Code Playgroud)