2016-06-16 101 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)只顯示水平或垂直線