删除或隐藏单元格中的值

问题描述 投票:0回答:2

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

Table image

你会发现,在将模拟数据添加到表格时,我非常有创意。

除了所有的笑话,我想从Priority列中删除所有值,因此只有彩色单元格。可能吗?也许迭代整个列并将值设置为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);
    } 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;
}

将渲染器分配给表:

TableColumn tblColumn = this.tblTodo.getColumnModel().getColumn(1);
    tblColumn.setCellRenderer(new PriorityColumnCellRenderer());
java swing user-interface jtable
2个回答
1
投票

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

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;
}

0
投票

找到答案。

超类中有一种方法:

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的答案

© www.soinside.com 2019 - 2024. All rights reserved.