在 Java 中从 TableModel 中删除列
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于您的表,请尝试调用 <代码>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.
是的。
不,但您可以编写自己的方法:
作为旁注,如果您想让用户能够隐藏/显示列,请查看 表列管理器。
Yes.
No, but you can make up your own method:
As a side note if you want to give the users the ability to hide/show columns check out the Table Column Manager.
如您所示,在
TableColumn
级别进行操作仅具有视觉影响,但对TableModel
没有任何影响。如果您想真正从
DefaultTableModel
中删除一列,那么您需要将其子类化,然后在您的子类中:我还没有检查它,但它应该适用于您的情况。
当然,
removeColumn()
只能从 EDT 中调用。请注意,我不会鼓励任何人编写此类代码;特别是,使用或派生
DefaultTableModel
并不是定义TableModel
的最佳解决方案。Acting at the
TableColumn
level, as you show, has only a visual impact but no impact on theTableModel
whatsoever.If you want to really remove a column from
DefaultTableModel
then you'll need to subclass it and then, in your subclass: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 aTableModel
.DefaultDataModel没有真正的removeColumn()函数,所以我自己写了一个函数,实际上可以解决问题。
The DefaultDataModel doesn't have a really removeColumn() function, so I wrote a function myself, which can actually solve the problem.