更改 jtable 行颜色

change jtable row color

我在自定义创建中的 jtable 自定义代码上使用此代码,但我选择的行没有更改请帮助我解决这个问题

    jtablexml = new javax.swing.JTable(){
public Component prepareRenderer ( TableCellRenderer renderer, int row, int column ){
    Component component = super.prepareRenderer(renderer,row,column);
    Object value = getModel().getValueAt(row,column);
    if(value.equals(null)){
        component.setBackground(Color.RED);
        component.setForeground(Color.BLACK);        }
    else{
        component.setBackground(Color.WHITE);
        component.setForeground(Color.BLACK);        }
        jtablexml.setSelectionBackground(Color.GREEN);
    return component;    }

};

but my selected row not changed

您的逻辑覆盖了行的默认选择。

您需要额外检查以确保默认情况下该行未突出显示:

Component component = super.prepareRenderer(renderer,row,column);

if (!isRowSelected(row))
{
    // add custom highlighting here
}

return component; 

有关其他行呈现示例,请参阅 Table Row Rendering

此外,不要尝试在渲染器中更改 table 的 属性。