2016-06-16 55 views
0

我试图在单元格之间显示一个JTable作为网格。我只能在单个单元格中添加边框,但是,这看起来不正确;如果我添加完整的边框,我会收到一堆断开的盒子,看起来很丑并且错误。使用MatteBorders(如下面的代码)看起来好一点,但会导致边界线不完全符合的空白。在JTable中添加边框BETWEEN单元格

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)一起工作。

+2

你已经试过'table.setShowGrid(真);'? – whiskeyspider

+0

@whiskeyspider这一直没有奏效,但我做了一些更深入的挖掘并找出原因 - 这只是默认为他们想要的每个单独的单元背景颜色。那+ setGridColor()解决了它,谢谢。 –

+0

默认行为是在单元格之间有网格线。阅读[如何使用表格](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)上的Swing教程部分。 – camickr

回答

2

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