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

Edw*_*ers 1 java swing border 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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

spg*_*ara 8

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