java 合并 JTable 中的单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/476721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merging cells in JTable
提问by Ameer Jewdaki
Is it possible to merge some cells of a JTable object?
是否可以合并 JTable 对象的某些单元格?

(source: codeguru.com)

(来源:codeguru.com)
If it's not possible through JTable what is the best approach. Thanks.
如果无法通过 JTable 实现,最好的方法是什么。谢谢。
采纳答案by Dave Ray
Not out-of-the-box. Here is an examplethat supports merging arbitrarty cells. This pagehas several examples of tables with spanning cells. Of course it's old and you get what you pay for. If paid software is an option, JIDE Gridshas some really nice Swing table support including custom cell spans.
不是开箱即用的。这是一个支持合并任意单元格的示例。此页面有几个带有跨单元格的表格示例。当然,它很旧,你得到你所支付的。如果付费软件是一个选项,JIDE Grids有一些非常好的 Swing 表支持,包括自定义单元格跨度。
回答by Pierre
You could implement a JTable using a TableModel merging two columns of the original TableModel.
您可以使用合并原始 TableModel 的两列的 TableModel 来实现 JTable。
class Model2 extends AbstractTableModel
{
private TableModel delegate;
public Model2(TableModel delegate)
{
this.delegate= delegate;
}
public int getRowCount() { return this.delegate.getRowCount();}
public int getColumnCount() { return this.delegate.getColumnCount()-1;}
public Object getValueAt(int row, int col)
{
if(col==0) return ""+delegate.getValueAt(row,col)+delegate.getValueAt(row,col+1);
return delegate.getValueAt(col+1);
}
(...)
}

