Swing JTable融合效果
1) JSTable extenends JTable, 重载rowAtPoint/columnAtPoint/getCellRect,覆盖被融合的部分
2) JSTableUI extends BasicTableUI,处理paintCell如下

private void paintCell(Graphics g, Rectangle cellRect, int row, int column) ...{
//定位为leading cell
MergeBlock mb=((JSTable)table).getMergeBlockManager().getMergeBlockOwner(row, column);
if(mb!=null )...{
if(mb.getC1()!=column && mb.getR1()!=row)...{
return;
}else...{
row=mb.getR1();
column=mb.getC1();
}
}
cellRect=table.getCellRect(row, column, false);
if (table.isEditing() && table.getEditingRow() == row
&& table.getEditingColumn() == column) ...{
Component component = table.getEditorComponent();
component.setBounds(cellRect);
component.validate();
} else ...{
//清除背景(格线)
Color c = g.getColor();
g.setColor(table.getBackground());
g.fillRect(cellRect.x,cellRect.y,cellRect.width,cellRect.height);
g.setColor(c);
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(renderer, row, column);
rendererPane.paintComponent(g, component, table, cellRect.x,
cellRect.y, cellRect.width, cellRect.height, true);
}
}
本文介绍了一种SwingJTable中实现单元格融合的方法。通过继承JTable并重写部分方法,同时自定义JSTableUI来处理单元格绘制逻辑。具体包括定位leading cell、调整单元格坐标及大小等细节。
2359

被折叠的 条评论
为什么被折叠?



