java JTable 将单元格颜色设置为特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25080951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:18:18  来源:igfitidea点击:

JTable Set Cell Color At Specific Value

javaswingjtabletablecellrenderer

提问by Xerath

I'm trying to write a method which for given parameters (value, color), sets color on the background of a cell which has value equal to cellValue.

我正在尝试编写一个方法,该方法对于给定的参数(值、颜色),在值等于 cellValue 的单元格的背景上设置颜色。

What my method actually does is, it sets color on the background of a cells for whole row and when I select the row on the table, and I want method to only set color at specific column (where cellValue is equal to value) each time I call the method.

我的方法实际上做的是,它在整行的单元格背景上设置颜色,当我选择表格上的行时,我希望方法每次只在特定列(其中 cellValue 等于值)设置颜色我调用方法。

    void setCellBackgroundColor(boolean cellValue, final Color color) {
        List<List<Object>> data = tView.getTTableModel().getData();

        for (int row = 0; row < data.size(); row++) {
            for (int col = 0; col < data.get(row).size(); col++) {
                TableCellRenderer renderer = tView.table.getCellRenderer(row, Col);
                Component component = tView.table.prepareRenderer(renderer, row, col);
                boolean bValue = 
                    TDataTypeRenderer.parseIntoRealValue(
                        data.get(row).get(col)
                    )
                );
                if (bValue == cellValue) {
                    component.setBackground(color);
                }
    }

回答by Braj

when I select the row on the table, and I want method to only set color at specific column

当我选择表格上的行时,我希望方法只在特定列设置颜色

Try with overridden prepareRenderer()method as suggested by @mKorbel.

尝试使用prepareRenderer()@mKorbel 建议的重写方法。

sample code:

示例代码:

Object[] columnNames = { "A", "B", "C", "D" };
Object[][] data = { 
        { "abc", new Double(850.503), 53, true },
        { "lmn", new Double(36.23254), 6, false }, 
        { "pqr", new Double(8.3), 7, false },
        { "xyz", new Double(246.0943), 23, true } };

JTable table = new JTable(data, columnNames) {
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
        Component comp = super.prepareRenderer(renderer, row, col);
        Object value = getModel().getValueAt(row, col);
        if (getSelectedRow() == row) {
            if (value.equals(false)) {
                comp.setBackground(Color.red);
            } else if (value.equals(true)) {
                comp.setBackground(Color.green);
            } else {
                comp.setBackground(Color.white);
            }
        } else {
            comp.setBackground(Color.white);
        }
        return comp;
    }
};

When selected first row:

选择第一行时:

enter image description here

在此处输入图片说明

When selected second row.

选择第二行时。

enter image description here

在此处输入图片说明

Read more...

阅读更多...



EDIT

编辑

As per your last comment

根据你最后的评论

Is it possible to change color with out clicking (selecting) row on the table?

是否可以在不单击(选择)表格行的情况下更改颜色?

Yes just remove the check of selected row.

是的,只需删除所选行的检查。

    Object value = getModel().getValueAt(row, col);
    if (value.equals(false)) {
        comp.setBackground(Color.red);
    } else if (value.equals(true)) {
        comp.setBackground(Color.green);
    } else {
       comp.setBackground(Color.white);
    }

enter image description here

在此处输入图片说明