如何从 CustomCellRenderer 为特定 JTable 单元调用 DefaulCellleRenderer

发布于 2024-09-05 20:25:13 字数 878 浏览 1 评论 0原文

我有一个类,其中有一个布尔字段。我在 JTable 中显示这些类的列表。 我创建了一个 CustomCellRenderer 来更改行的背景颜色,这样我就可以有不同的颜色。

问题:当自定义渲染器应用于布尔字段时,会渲染 (true/false) 而不是默认渲染器的复选框。

我怎样才能同时拥有这两个功能:背景颜色和复选框?

这是customrenderer代码:

public class CustomCellRenderer extends DefaultTableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object obj,boolean isSelected, boolean hasFocus, int row, int column) {

        Component cell = super.getTableCellRendererComponent(table, obj,isSelected, hasFocus, row, column);

        if (isSelected) {
            cell.setBackground(Color.red);
        } else {
            if (row % 2 == 0) {
                cell.setBackground(new Color(110,134,214));

            } else {
                cell.setBackground(Color.lightGray);

            }
        }
        return cell;
    }
}

提前感谢您的帮助。

I have a class that has a Boolean field in it. I display in a JTable a list of of those classes.
I created a CustomCellRenderer to change the background color of the rows, so I could have different colors.

the problem: when the customrenderer is applied on the Boolean field, (true/false) is rendered instead of the default renderer's checkbox.

how can I have both features: background colors and checkbox?

here is the customrenderer code:

public class CustomCellRenderer extends DefaultTableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object obj,boolean isSelected, boolean hasFocus, int row, int column) {

        Component cell = super.getTableCellRendererComponent(table, obj,isSelected, hasFocus, row, column);

        if (isSelected) {
            cell.setBackground(Color.red);
        } else {
            if (row % 2 == 0) {
                cell.setBackground(new Color(110,134,214));

            } else {
                cell.setBackground(Color.lightGray);

            }
        }
        return cell;
    }
}

thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2024-09-12 20:25:13

我得到了它。

我发现了这个:

仅向 JTable 中的一个单元格添加一个复选框

我使用了从 CustomCellRenderer 的 getTableCellRendererComponent 方法调用的 BooleanRenderer。

我刚刚在这个方法中添加了一行委托渲染:

public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        //delegate if boolean
        if(value instanceof Boolean) return booleanRenderer.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
        if (isSelected) {
            setBackground(selectedColor);
            setForeground(unSelectedColorWhite);
        } else {
            if (row % 2 == 0) {
                setBackground(unSelectedColorBlue);
            } else {
                setBackground(unSelectedColorWhite);
            }
            setForeground(selectedColor);
        }
        setText(" " + table.getValueAt(row, column)); 
        selected = isSelected;

        return this;
    }

I got it.

I found this :

add a check box to only one cell in a JTable

I used the BooleanRenderer that I called from the getTableCellRendererComponent method of the CustomCellRenderer.

I just added a line in this method the delegate the rendering:

public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        //delegate if boolean
        if(value instanceof Boolean) return booleanRenderer.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
        if (isSelected) {
            setBackground(selectedColor);
            setForeground(unSelectedColorWhite);
        } else {
            if (row % 2 == 0) {
                setBackground(unSelectedColorBlue);
            } else {
                setBackground(unSelectedColorWhite);
            }
            setForeground(selectedColor);
        }
        setText(" " + table.getValueAt(row, column)); 
        selected = isSelected;

        return this;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文