在 Java 中从 TableModel 中删除列

发布于 2024-11-06 03:22:33 字数 833 浏览 2 评论 0原文

在 Java 中,我使用 DefaultTableModel 动态地将列添加到 JTable。

//create DefaultTableModel with columns and no rows
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(tableModel);

columnNames 变量是一个包含列名称的字符串数组。因此,在程序启动并运行后,用户可以选择添加其他列。我这样做如下,

tableModel.addColumn("New column name");

根据需要动态地将列添加到表中。用户还可以删除添加的列。为此,我使用以下代码:

          TableColumn tcol = table.getColumnModel().getColumn(0);
          table.getColumnModel().removeColumn(tcol);

应该删除指定索引处的列,我也尝试过:

table.removeColumn(sheet.getColumn(assessmentName));

它们都可以(视觉上)工作,但这就是问题。删除添加的列后,如果添加另一列并且刷新表,则之前删除的列又会出现。因此,虽然它在视觉上删除了该列,但最后两个代码片段实际上都没有将其从模型中删除。我在这里假设,由于该列已添加到模型中,因此需要从中删除它?是否需要调用特定方法或需要实现一些逻辑来删除该列?

In Java I'm using the DefaultTableModel to dynamically add a column to a JTable.

//create DefaultTableModel with columns and no rows
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(tableModel);

The columnNames variable is a string array with the column names. So after the program is up and running the user has the option to add additional columns. I do so as follows

tableModel.addColumn("New column name");

Which dynamically adds the column to the table as desired. The user can also remove columns added. For this I use the following code:

          TableColumn tcol = table.getColumnModel().getColumn(0);
          table.getColumnModel().removeColumn(tcol);

which should remove the column at a specified index, I've also tried:

table.removeColumn(sheet.getColumn(assessmentName));

Both of them work (visually), but here's the problem. After deleting an added column, if another column is added and the table refreshes, the previously deleted column is there again. So while it is removing the column visually, neither of the last two code snippets actually removes it from the model. I'm assuming here that since the column was added to the model that is where it needs to be removed from? Is there a specific method that I need to call or some logic that I need to implement to remove the column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

‖放下 2024-11-13 03:22:33

对于您的表,请尝试调用 <代码>table.setAutoCreateColumnsFromModel(false);

这篇文章有一个关于如何删除列和基础数据的很好的例子。

For your table, try calling table.setAutoCreateColumnsFromModel(false);

This post has a good example as to how to delete column and the underlying data.

妖妓 2024-11-13 03:22:33

我在这里假设,由于该列已添加到模型中,因此需要从中删除它?

是的。

是否有我需要调用的特定方法或需要实现的一些逻辑来删除该列?

不,但您可以编写自己的方法:

moveColumn(...); // to move the column to the end
setColumnCount(...); // to remove the last column

作为旁注,如果您想让用户能够隐藏/显示列,请查看 表列管理器

I'm assuming here that since the column was added to the model that is where it needs to be removed from?

Yes.

Is there a specific method that I need to call or some logic that I need to implement to remove the column?

No, but you can make up your own method:

moveColumn(...); // to move the column to the end
setColumnCount(...); // to remove the last column

As a side note if you want to give the users the ability to hide/show columns check out the Table Column Manager.

疏忽 2024-11-13 03:22:33

如您所示,在 TableColumn 级别进行操作仅具有视觉影响,但对 TableModel 没有任何影响。

如果您想真正从 DefaultTableModel 中删除一列,那么您需要将其子类化,然后在您的子类中:

public class MyTableModel extends DefaultTableModel {
    public void removeColumn(int column) {
        columnIdentifiers.remove(column);
        for (Object row: dataVector) {
            ((Vector) row).remove(column);
        }
        fireTableStructureChanged();
    }
}

我还没有检查它,但它应该适用于您的情况。

当然,removeColumn() 只能从 EDT 中调用。

请注意,我不会鼓励任何人编写此类代码;特别是,使用或派生 DefaultTableModel 并不是定义 TableModel 的最佳解决方案。

Acting at the TableColumn level, as you show, has only a visual impact but no impact on the TableModel whatsoever.

If you want to really remove a column from DefaultTableModel then you'll need to subclass it and then, in your subclass:

public class MyTableModel extends DefaultTableModel {
    public void removeColumn(int column) {
        columnIdentifiers.remove(column);
        for (Object row: dataVector) {
            ((Vector) row).remove(column);
        }
        fireTableStructureChanged();
    }
}

I haven't checked it, but it should work in your case.

Of course, removeColumn() should be called only from the EDT.

Note that I wouldn't encourage anyone to produce this kind of code; in particular, using, or deriving from, DefaultTableModel is not the best solution to define a TableModel.

你的他你的她 2024-11-13 03:22:33

DefaultDataModel没有真正的removeColumn()函数,所以我自己写了一个函数,实际上可以解决问题。

private void removeColumn(int index, JTable myTable){
    int nRow= myTable.getRowCount();
    int nCol= myTable.getColumnCount()-1;
    Object[][] cells= new Object[nRow][nCol];
    String[] names= new String[nCol];

    for(int j=0; j<nCol; j++){
        if(j<index){
            names[j]= myTable.getColumnName(j);
            for(int i=0; i<nRow; i++){
                cells[i][j]= myTable.getValueAt(i, j);
            }
        }else{
            names[j]= myTable.getColumnName(j+1);
            for(int i=0; i<nRow; i++){
                cells[i][j]= myTable.getValueAt(i, j+1);
            }
        }
    }

    DefaultTableModel newModel= new DefaultTableModel(cells, names);
    myTable.setModel(newModel);       
}

The DefaultDataModel doesn't have a really removeColumn() function, so I wrote a function myself, which can actually solve the problem.

private void removeColumn(int index, JTable myTable){
    int nRow= myTable.getRowCount();
    int nCol= myTable.getColumnCount()-1;
    Object[][] cells= new Object[nRow][nCol];
    String[] names= new String[nCol];

    for(int j=0; j<nCol; j++){
        if(j<index){
            names[j]= myTable.getColumnName(j);
            for(int i=0; i<nRow; i++){
                cells[i][j]= myTable.getValueAt(i, j);
            }
        }else{
            names[j]= myTable.getColumnName(j+1);
            for(int i=0; i<nRow; i++){
                cells[i][j]= myTable.getValueAt(i, j+1);
            }
        }
    }

    DefaultTableModel newModel= new DefaultTableModel(cells, names);
    myTable.setModel(newModel);       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文