Java着色表行 [英] Java Coloring a Table row

查看:115
本文介绍了Java着色表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据第3列中是否存在非null来为Table行着色. 这是我编写的代码:(忽略花括号)

I want to color a Table row depending on whether there is a non-null in its 3rd column. Heres the code that I wrote : (ignore the braces)

public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {

        JComponent c =(JComponent) super.prepareRenderer(renderer, row, column);
        c.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
        if (column == 2 ){

                String value = (String) getValueAt(row, column);
                System.out.println(value);

                if (! value.equals(null)){

                    c.setForeground(Color.RED);
                }
            } 

问题是当我执行此操作时,即使在第三列中只有一行具有非空值,表的所有行也都变为彩色. 我要去哪里错了?

The problem is when I execute this all the rows of the table get colored even if only 1 row has a non-null value in the 3rd column. Where am I going wrong?

推荐答案

JTable的默认呈现器是跨多个单元共享的整个表的单个实例.设置好前景后,便会对其进行所有使用.如果该值不为null,则应将其设置回默认颜色.另外,为什么要使用.equals(null)而不是== null?

JTable's default renderer is a single instance for the entire table shared across multiple cells. Once you're setting the foreground, it will be set for all usages of it. You should set it back to the default color when the value is not null. Also, why are you using .equals(null) instead of == null?

这篇关于Java着色表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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