删除 jtable 中的单元格边框

发布于 2024-09-07 14:59:54 字数 921 浏览 4 评论 0原文

我有自定义单元格渲染器,并且想要删除单元格的边框。
我该怎么做?我尝试了 setBorder 但它不起作用。

这是我的渲染器代码:

public class MyTableCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = -1195682136616306875L;

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        Component c = super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, column);
        if (!isSelected) {
            if (row % 2 == 0 && row != 1) {
                c.setBackground(new Color(255, 255, 150));
            } else {
                c.setBackground(Color.WHITE);
            }
        } else {
            c.setBackground(new Color(255, 230, 255));
        }
        c.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        return c;
    }
}

I have my on custom cell renderer and want to remove the border of the cell.
How can i do it? I tried setBorder but it doesn't work.

Here is my renderer code:

public class MyTableCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = -1195682136616306875L;

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        Component c = super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, column);
        if (!isSelected) {
            if (row % 2 == 0 && row != 1) {
                c.setBackground(new Color(255, 255, 150));
            } else {
                c.setBackground(Color.WHITE);
            }
        } else {
            c.setBackground(new Color(255, 230, 255));
        }
        c.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        return c;
    }
}

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

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

发布评论

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

评论(2

活雷疯 2024-09-14 14:59:54

单元格之间绘制的线不是单元格本身的一部分。它们是由表格绘制的。您可以使用以下方法为整个表格关闭它们:

table.setShowGrid(false);

仅禁用水平线或垂直线:

table.setShowHorizontalLines(false);
table.setShowVerticalLines(false);

或者,您可以使用以下方法更改线条的颜色:

table.setGridColor(color)

The lines drawn between cells are not part of the cells themselves. They are drawn by the table. You can turn them off for the entire table with:

table.setShowGrid(false);

To disable just the the horizontal or just the vertical lines:

table.setShowHorizontalLines(false);
table.setShowVerticalLines(false);

Or, you can change the color of the lines with:

table.setGridColor(color)
深海蓝天 2024-09-14 14:59:54

我不知道你的代码是如何编译的,因为只有 Swing 组件可以有 Border,而 Component 类没有 setBorder() 方法。

当我覆盖默认渲染器时,我使用如下内容:

Class CustomRenderer extends DefaultTableCellRenderer
{
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        this.setBorder (BorderFactory.createBevelBorder (EtchedBorder.RAISED));
        return this;
    }
}

I don't know how your code compiles since only Swing components can have a Border and the Component class doesn't have a setBorder() method.

When I override the default renderer I use something like:

Class CustomRenderer extends DefaultTableCellRenderer
{
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        this.setBorder (BorderFactory.createBevelBorder (EtchedBorder.RAISED));
        return this;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文