尝试使用自定义渲染器为JTable的特定行着色,相反,我所有的行都被着色了 [英] Trying to color specific rows of JTable using Custom Renderer, instead all my rows are colored

查看:208
本文介绍了尝试使用自定义渲染器为JTable的特定行着色,相反,我所有的行都被着色了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Java程序,基本上,当JTable的第4列中的值大于第3列时,我希望这些特定的行被涂成红色而不是其他行.

for my Java program basically when the value in column 4 of my JTable is greater than column 3, I want those specific rows to be colored in red and not the other rows.

我已经实现了以下代码,但是由于某种原因,我的所有行都变成了红色,而不仅仅是符合条件的行.

I have implemented the following code, but for some reason all my rows are getting colored in red rather than just the ones matching the criteria.

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
    @Override
    public Component getTableCellRendererComponent(JTable table, 
                   Object value, boolean isSelected, boolean hasFocus, int row, int col) {

        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);

        int Value1= Integer.parseInt(table.getModel().getValueAt(row, 3).toString());
        int Value2= Integer.parseInt(table.getModel().getValueAt(row, 4).toString());
        if (Value2>=Value1) {                        
            setBackground(Color.red);
        } 
        return this;
    }   
});

有关如何解决此问题的任何建议/提示?

Any suggestions/tips on how to fix this?

推荐答案

A

A DefaultTableCellRenderer instance uses a template component to render all cells (namely itself, see documentation). Once you set its color, the template will have that color and will be applied to all subsequent cells.

您需要做的是根据您的逻辑,在需要的情况下将颜色设置为红色,在所有其他情况下将其设置为默认背景颜色.

What you need to do is in your logic, set the color to red in the cases you need, and set it to the default background color in all other cases.

if(!isSelected) {
    if (Value2>=Value1) {                        
        setBackground(Color.red);
    } else {
        setBackground(table.getBackground()); // or use another color for another background
    }
}


再次查看您的代码,我注意到您在模型索引和视图索引方面犯了一个错误. getTableCellRendererComponent方法与视图索引一起调用,但是您正在使用这些索引为模型建立索引(例如,在table.getModel().getValueAt(row, 3)中).对表进行排序后,结果将不正确,因为模型索引和视图索引将不同.


Looking at your code again, I'm noticing you are making an error with regards to model versus view indices. The getTableCellRendererComponent method is called with view indices, yet you are using these to index the model (eg in table.getModel().getValueAt(row, 3)). When your table is sorted, results will be incorrect as model indices and view indices will differ.

如果需要从模型中获取值,则首先需要将视图索引转换为模型索引.使用 JTable.convertRowIndexToModel JTable.convertColumnIndexToModel 做到这一点.例如:

If you need to get values from the model, you first need to convert the view indices to model indices. Use JTable.convertRowIndexToModel and JTable.convertColumnIndexToModel to do that. Eg:

int modelRowId = table.convertRowIndexToModel(row);
int Value1= Integer.parseInt(table.getModel().getValueAt(modelRowId, 3).toString());
int Value2= Integer.parseInt(table.getModel().getValueAt(modelRowId, 4).toString());

这篇关于尝试使用自定义渲染器为JTable的特定行着色,相反,我所有的行都被着色了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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