删除或隐藏单元格中的值

Removing or hiding values in a cell

有没有办法隐藏或删除某个单元格或整列的值? 如果你看看这张照片:

你会发现我在向 table 添加模拟数据时非常有创意。

撇开所有笑话不谈,我想从“优先级”列中删除所有值,因此只有彩色单元格。可能吗?也许遍历整个列并将值设置为空?只是一个想法,没有实现计划...

编辑

我的单元格渲染器:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

    if(value.equals("1")) { // red
        cell.setBackground(Color.RED);
        cell.setForeground(Color.WHITE);
    } else if (value.equals("2")) { // yellow
        cell.setBackground(Color.ORANGE);
        cell.setForeground(Color.WHITE);
    } else if (value.equals("3")) { // green
        cell.setBackground(Color.GREEN);
        cell.setForeground(Color.WHITE);
    } else { // white
        cell.setBackground(Color.WHITE);
        cell.setForeground(Color.GRAY);
    }

    return cell;
}

正在将渲染器分配给 table:

TableColumn tblColumn = this.tblTodo.getColumnModel().getColumn(1);
    tblColumn.setCellRenderer(new PriorityColumnCellRenderer());

您可以简单地将渲染器的 getTableCellRendererComponent(...) 方法中的超级方法调用中 value 的值更改为空字符串:"":

Component cell = super.getTableCellRendererComponent(table, "", isSelected, 
        hasFocus, row, column);

例如,

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Component cell = super.getTableCellRendererComponent(table, "", isSelected, 
        hasFocus, row, column);

    if(value.equals("1")) { // red
        cell.setBackground(Color.RED);
        cell.setForeground(Color.WHITE);
    } else if (value.equals("2")) { // yellow
        cell.setBackground(Color.ORANGE);
        cell.setForeground(Color.WHITE);
    } else if (value.equals("3")) { // green
        cell.setBackground(Color.GREEN);
        cell.setForeground(Color.WHITE);
    } else { // white
        cell.setBackground(Color.WHITE);
        cell.setForeground(Color.GRAY);
    }
    return cell;
}

找到答案。

super中有方法class:

super.setValue("myValue");

在我的例子中,它看起来像这样:

super.setValue(null);

但是这个需要检查值后调用。最终代码:

编辑

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

    if(value.equals("1")) { // red
        cell.setBackground(Color.RED);
        cell.setForeground(Color.WHITE);
        super.setValue(null);
    } else if (value.equals("2")) { // yellow
        cell.setBackground(Color.ORANGE);
        cell.setForeground(Color.WHITE);
        super.setValue(null);
    } else if (value.equals("3")) { // green
        cell.setBackground(Color.GREEN);
        cell.setForeground(Color.WHITE);
        super.setValue(null);
    } else { // white
        cell.setBackground(Color.WHITE);
        cell.setForeground(Color.GRAY);
        super.setValue(null);
    }

    return cell;
}

编辑

为简单起见,我使用了Hovercraft Full Of Eels的回答