简体   繁体   English

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

[英]Add border BETWEEN cells in JTable

I'm trying to display a JTable as a grid, with lines between the cells. 我试图将JTable显示为网格,单元格之间有线。 I've only been able to add borders within individual cells, though, which never looks correct; 不过,我只能在单个单元格中添加边框,但这看上去永远都不正确。 if I add full borders, I get a bunch of disconnected boxes, which looks ugly and wrong. 如果添加完整边框,则会得到一堆未连接的框,这看起来很丑陋和错误。 Using MatteBorders (as the below code) looks a little better, but results in gaps where border lines don't quite meet. 使用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;
}

I feel like there must be some way to do this properly, so that I just get grid lines between cell elements. 我觉得必须有某种方法可以正确地做到这一点,这样我才可以在单元格元素之间获得网格线。 What am I missing? 我想念什么? If nothing else, is there a way to get MatteBorder to stretch across the gaps, or to push a normal border out slightly further so that the borders of adjacent cells overlap? 如果没有其他方法,是否有办法使MatteBorder跨过间隙伸展,或将正常边界稍微向外推,以使相邻单元的边界重叠?

EDIT: Got it working with setShowGrid(true) and setGridColor(Color.BLACK). 编辑:使其与setShowGrid(true)和setGridColor(Color.BLACK)一起使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM