2014-03-27 32 views
2

我有一个jxtable。它有horizontalGridLines enabled。这是它的样子。jxtable与更厚的网格线

Current JXTable

我想要的水平网格线要厚。看到下面的期望看。第二排之后的线条应该有较厚的分隔线。

enter image description here

回答

2

您可以覆盖JXTable的paintComponent方法。下面的示例创建一个JTable与第二行之后的3个像素的线厚度:

JXTable table = new JXTable() { 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     // Actual line thickness is: thickness * 2 + 1 
     // Increase this as you wish. 
     int thickness = 1; 

     // Number of rows ABOVE the thick line 
     int rowsAbove = 2; 

     g.setColor(getGridColor()); 
     int y = getRowHeight() * rowsAbove - 1; 
     g.fillRect(0, y - thickness, getWidth(), thickness * 2 + 1); 
    }; 
}; 
+1

+1一个黑客和另一个一样好(或坏:-)。一些建议:a)增加相邻行之一的rowHeight,否则该行可能被绘制在内容b)上绘制位置,查询表格(通过getCellRect())而不是手动计算 – kleopatra

+0

@kleopatra我原本有使用'getCellRect()'的代码,但我改变了它以使代码更简单。至于增加rowHeight,这将工作,但它会看起来奇怪与2个不平衡的行。感谢您的反馈。 – uyuyuy99

+0

_make代码simple_和脆;-) – kleopatra

1

网格线的画由表格的UI-代表控制。没有办法干涉,所有选项都是黑客。

这就是说:如果目标行是第二行,SwingX'sh hack将使用用MatteBorder装饰渲染器的荧光笔。

table.setShowGrid(true, false); 
// apply the decoration for the second row only 
HighlightPredicate pr = new HighlightPredicate() { 

    @Override 
    public boolean isHighlighted(Component renderer, ComponentAdapter adapter) { 
     return adapter.row == 1; 
    } 
}; 
int borderHeight = 5; 
// adjust the rowHeight of the second row 
table.setRowHeight(1, table.getRowHeight() + borderHeight); 
Border border = new MatteBorder(0, 0, borderHeight, 0, table.getGridColor()); 
// a BorderHighlighter using the predicate and the MatteBorder 
Highlighter hl = new BorderHighlighter(pr, border); 
table.addHighlighter(hl);