JTable将单元格颜色设置为特定值 [英] JTable Set Cell Color At Specific Value

查看:68
本文介绍了JTable将单元格颜色设置为特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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等于value) ),每次调用该方法时.

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.

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

推荐答案

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

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

尝试使用@mKorbel建议的覆盖的prepareRenderer()方法.

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

示例代码:

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

当选择第一行:

当选择第二行.

了解更多...

根据您的最新评论

是否可以在不单击表的行的情况下更改颜色?

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

这篇关于JTable将单元格颜色设置为特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆