3

我目前只能在最后一列中添加一列,并删除添加的最后一列。

我试图弄清楚如何添加或删除选定的列,但它只是不适合我。例如,如果我有 3 列 0、1 和 2,并且我想在 1 中添加一列或删除第 1 列。

我在图书馆里呆了一个小时,但我能做的最多的是删除行并将行添加到选定的位置。

有人可以帮忙吗?

这是我添加列的内容,但它没有做我想要的(只添加到最后):

String colName = Integer.toString(i++);
        if (colName != null && colName.length() > 0) {

            model.addColumn(colName);
            table.updateUI();
            undo.push(new Object[]{"Column", "Add", colName});
            redo.clear(); 
        }
4

1 回答 1

5

要在任意索引处添加列,请使用表列模型,addColumn()后跟moveColumn()

TableColumn newColumn = // ...
colModel.addColumn(newColumn);
colModel.moveColumn(colModel.getColumnCount() - 1, desiredIndex);

删除索引处的列应该更容易:

colModel.removeColumn(colModel.getColumn(desiredIndex));
于 2012-07-24T19:36:30.387 回答