【发布时间】:2014-05-07 01:38:17
【问题描述】:
我有一个jxtable。它有horizontalGridLines enabled。这是它的样子。
我希望水平网格线更粗。请参阅下面的所需外观。第二行之后的线应该有一个更粗的分隔线。
【问题讨论】:
标签: java swing jtable swingx jxtable
我有一个jxtable。它有horizontalGridLines enabled。这是它的样子。
我希望水平网格线更粗。请参阅下面的所需外观。第二行之后的线应该有一个更粗的分隔线。
【问题讨论】:
标签: java swing jtable swingx jxtable
您可以覆盖 JXTable 中的paintComponent 方法。以下示例在第 2 行之后创建一个线宽为 3 像素的 JTable:
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);
};
};
【讨论】:
getCellRect() 的代码,但我对其进行了更改以使代码更简单。至于增加 rowHeight,这会起作用,但如果有 2 个不平衡的行,它看起来会很奇怪。顺便感谢您的反馈。
网格线的绘制由表格的 ui-delegate 控制。没有办法干涉,所有的选择都是黑客。
也就是说:如果目标行是第二个,SwingX'sh hack 将使用一个 Highlighter,它用一个 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);
【讨论】: