尝试在JTable中为特定单元格着色... getTableCellRendererComponent Overide [英] Trying to color specific cell in JTable... getTableCellRendererComponent Overide

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

问题描述

所以我知道这可能是一个重复的问题,但我已经浏览了许多已经存在的问题,而且似乎没有一个适合我,所以我想我会发布自己的,希望其他的一些遇到此问题的人会发现这也很有用。

So I know this may be a duplicate question, but I've looked through many of the ones already on here and none of them seem to work for me, so I thought I would post my own and hopefully some of the other people having trouble with this will find this helpful also.

这是我的代码

    table.getColumn("Name").setCellRenderer(
                new DefaultTableCellRenderer() {
                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        setText(value.toString());

                        if (row==3) 
                        {
                            setForeground(Color.RED);
                        }
                        return this;
                    }
                }
            );

以下是JFrame中显示的内容。正如您所看到的,我正在尝试仅对名称列的第三行中的文本着色,但它会为整行着色。

Here is what is displayed in the JFrame. As you can see I am trying to to only color the text in the third row of the Column "Name" but it colors the whole row.

任何建议?
谢谢!
Canaan

Any suggestions? Thanks! Canaan

推荐答案

渲染对于名称列是唯一的。当行为3时,您将红色设置为前景色,但是您不为其他行重置它,因此当调用画家时,它总是涂成红色。
当行为3时你必须设置红色,但在其他情况下你还必须重置原始颜色。

The render is unique for column "Name". You are setting Red as foreground color when row is 3 but you dont reset it for others rows, so when painter is called it always paint red. You have to set red when row is 3 but you also have to reset the original color in other case.

编辑:执行版本。现在备份原始前景色,而super用于像其他列一样呈现。

EDITED: Performed version. Now original foreground color is backed up, and super is used to render like others columns.

           table.getColumn("Name").setCellRenderer(
            new DefaultTableCellRenderer() {

        Color originalColor = null;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (originalColor == null) {
                originalColor = getForeground();
            }
            if (value == null) {
                renderer.setText("");
            } else {
                renderer.setText(value.toString());
            }

            if (row == 3) {
                renderer.setForeground(Color.RED);
            } else {
                renderer.setForeground(originalColor); // Retore original color
            }
            return renderer;
        }
    });

这篇关于尝试在JTable中为特定单元格着色... getTableCellRendererComponent Overide的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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