在 Java 中从 TableModel 中删除列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5938436/
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
Removing Column from TableModel in Java
提问by Mark
In Java I'm using the DefaultTableModel to dynamically add a column to a JTable.
在 Java 中,我使用 DefaultTableModel 将一列动态添加到 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
columnNames 变量是一个包含列名的字符串数组。因此,在程序启动并运行后,用户可以选择添加其他列。我这样做如下
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?
它们都工作(在视觉上),但这是问题所在。删除添加的列后,如果添加另一列并且表刷新,则先前删除的列又存在。因此,虽然它在视觉上删除了列,但最后两个代码片段实际上都没有从模型中删除它。我在这里假设,由于该列已添加到模型中,因此需要从中删除它?是否有我需要调用的特定方法或我需要实现的某些逻辑来删除列?
采纳答案by Bala R
For your table, try calling table.setAutoCreateColumnsFromModel(false);
对于您的餐桌,请尝试致电 table.setAutoCreateColumnsFromModel(false);
This posthas a good example as to how to delete column and the underlying data.
这篇文章有一个很好的例子来说明如何删除列和底层数据。
回答by jfpoilpret
Acting at the TableColumn
level, as you show, has only a visual impact but no impact on the TableModel
whatsoever.
TableColumn
正如你所展示的,在这个层面上表演只会产生视觉冲击,但对TableModel
任何事物都没有影响。
If you want to really remove a column from DefaultTableModel
then you'll need to subclass it and then, in your subclass:
如果您想真正从中删除一列,DefaultTableModel
则需要对其进行子类化,然后在您的子类中:
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.
当然,removeColumn()
应该只从 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
.
请注意,我不鼓励任何人生成这种代码;特别是,使用或派生自DefaultTableModel
不是定义TableModel
.
回答by camickr
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.
作为旁注,如果您想让用户能够隐藏/显示列,请查看表列管理器。
回答by Kailang Jiang
The DefaultDataModel doesn't have a really removeColumn() function, so I wrote a function myself, which can actually solve the problem.
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);
}