[英]Java Swing - Multiple column headers in a JTable?
Is there any way to create multiple column headers in a JTable
? 有没有办法在
JTable
创建多个列标题? I mean, normally there is only one row, but I need two of them with the same format (headerlike), and combine some cells of one of those headers. 我的意思是,通常只有一行,但我需要两个具有相同格式(标题),并组合其中一个标题的一些单元格。
I need something like this: 我需要这样的东西:
Header0 | Header123 | Header4 Header0 | Header1 | Header2 | Header3 | Header4
Is there any way? 有什么办法吗?
Nick Meyer thanks for kindly reply, while the content in your address is a little out of date. Nick Meyer感谢您的回复,而您地址中的内容有点过时了。 i run it with jre 1.7 and it didn't works as expected but it can be altered to work correct.
我用jre 1.7运行它并没有按预期工作但它可以改变工作正确。 the alter i made are as follow
我做的改变如下
/*
* add these code in GroupableTableHeader
*/
public void updateUI(){
// setUI(this.getUI());
TableCellRenderer renderer = getDefaultRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
}
}
/*
* add these code in GroupableTableHeaderUI in 2 places, you must know where
*/
if (renderer == null) {
renderer = header.getDefaultRenderer();
}
/*
* change the getSize method in ColumnGroup
*/
public Dimension getSize(JTable table) {
Component comp = renderer.getTableCellRendererComponent(
table, getHeaderValue(), false, false,-1, -1);
int height = comp.getPreferredSize().height;
int width = 0;
Enumeration en = v.elements();
while (en.hasMoreElements()) {
Object obj = en.nextElement();
if (obj instanceof TableColumn) {
TableColumn aColumn = (TableColumn)obj;
width += aColumn.getWidth();
// width += margin;
} else {
width += ((ColumnGroup)obj).getSize(table).width;
}
}
return new Dimension(width, height);
}
and the finally results. 最后的结果。
Groupable Header示例是一些可能能够帮助您的旧代码。
you can extend BasicTableHeaderUI
and write your own implementation of paint method, in which you can draw any type of header. 你可以扩展
BasicTableHeaderUI
并编写自己的paint方法实现,在其中你可以绘制任何类型的头。 after this change default header UI with following command table.getTableHeader().setUI(MyTableHeaderUI)
在此更改之后使用以下命令更改默认标头UI
table.getTableHeader().setUI(MyTableHeaderUI)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.