在 JTable 中的单元格之间添加边框

Add border BETWEEN cells in JTable

我正在尝试将 JTable 显示为网格,单元格之间有线条。不过,我只能在单个单元格内添加边框,这看起来永远不正确;如果我添加完整的边框,我会得到一堆断开连接的框,看起来很难看而且是错误的。使用 MatteBorders(如以下代码)看起来好一些,但会导致边界线不完全相交的间隙。

public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
    Component stamp = super.prepareRenderer(renderer, row, column);
    int top = 1;
    int left = 1;
    int bottom = row == 7 ? 1 : 0; //Grid is always 8x8, this ensures the bottom/right will have full borders.
    int right = column == 7 ? 1 : 0;
    MatteBorder border = new MatteBorder(top, left, bottom, right, Color.BLACK);
    if (stamp instanceof JComponent) {
        ((JComponent) stamp).setBorder(border);
    }
    return stamp;
}

我觉得必须有某种方法可以正确地做到这一点,这样我就可以在单元格元素之间得到网格线。我错过了什么?如果不出意外,有没有办法让 MatteBorder 跨越间隙,或者将正常边框稍微向外推一点,以便相邻单元格的边框重叠?

编辑:让它与 setShowGrid(true) 和 setGridColor(Color.BLACK) 一起工作。

使用 JTable.setShowGrid(true) 显示默认边框或使用 setShowHorizontalLines(boolean showHorizontalLines)setShowVerticalLines(boolean showVerticalLines) 仅显示水平或垂直线