且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在DefaultTableModel / JTable中合并单元格?

更新时间:2023-12-03 11:52:40

,遗憾的是它不是免费的。这是例如 CellSpanTable




I searched a lot and got some answers for this Q. but many of them referred to links which give 404 error. I want to make table like this:

Is there any method in java for this?

MultiSpanCellTableExample demonstrates how to merge cells by creating a custom TableUI. There seem to be a problem in this example that causes ***Error, at least in Java 6. To fix this, inside AttributiveCellTableModel.setDataVector() replace:

setColumnIdentifiers(columnNames);

with:

this.columnIdentifiers = columnNames;

IE:

public void setDataVector(Vector newData, Vector columnNames) {
    if (newData == null)
        throw new IllegalArgumentException(
                "setDataVector() - Null parameter");
    dataVector = new Vector(0);
    // setColumnIdentifiers(columnNames);
    this.columnIdentifiers = columnNames;
    dataVector = newData;

    cellAtt = new DefaultCellAttribute(dataVector.size(),
            columnIdentifiers.size());

    newRowsAdded(new TableModelEvent(this, 0, getRowCount() - 1,
            TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}

The problem is that setColumnIdentifiers calls into setDataVector hence triggering the ***Error. Once fixed, this is how the example looks like:

There is also a ready solution from JIDE, unfortunately it is not free. Here is for example CellSpanTable: