如何将列插入/删除到 JTable java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19758002/
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
How to insert/delete column to JTable java
提问by CROSP
I have no ideas what to do. I am creating an application. I need to work with the table, so I am using JTable. But I have a lot problems with it.
It seems to work, but when I try to delete column it this column disappears (only in GUI), but all the information still exists. Also columncount doesn't change.
I have searched and tried a lot of different code, but nothing changed.
我不知道该怎么做。我正在创建一个应用程序。我需要处理表格,所以我使用 JTable。但我有很多问题。它似乎有效,但是当我尝试删除列时,该列消失了(仅在 GUI 中),但所有信息仍然存在。列数也不会改变。
我搜索并尝试了很多不同的代码,但没有任何改变。
public void addTblCol(JTable table,String name) {
DefaultTableModel model = (DefaultTableModel)table.getModel();
TableColumn col = new TableColumn(model.getColumnCount());
col.setHeaderValue(name);
table.addColumn(col);
model.addColumn(name);
this.realColCnt++;
};
public void delTblCol(JTable table,int index) {
DefaultTableModel model = (DefaultTableModel)table.getModel();
TableColumn col = table.getColumnModel().getColumn(index);
table.removeColumn(col);
table.revalidate();
this.realColCnt--;
};
采纳答案by camickr
The DefaultTableModel
supports a setColumnCount()
method which effectively will allow you to remove columns from the end of the model only.
该DefaultTableModel
支持setColumnCount()
,有效地将允许你只从模式的终结删除列的方法。
If you want to remove columns from the middle of the model, then you will need to:
如果要从模型中间删除列,则需要:
- extend the
DefaultTableModel
and create your ownremoveColumn(int column)
method. - This method would need to loop through every row in the Vector and use the
Vector.remove(int)
method to remove the column for ever row. - Finally once this is done you would need to invoke the
fireTableStructureChanged()
method to tell the table that a column has been removed so the table can be repainted.
- 扩展
DefaultTableModel
并创建您自己的removeColumn(int column)
方法。 - 此方法需要遍历 Vector 中的每一行,并使用该
Vector.remove(int)
方法删除每一行的列。 - 最后,一旦完成,您将需要调用该
fireTableStructureChanged()
方法来告诉表已删除一列,以便可以重新绘制表。
回答by Hovercraft Full Of Eels
Some general information related to your question.
与您的问题相关的一些一般信息。
The JTable APIfor public void removeColumn(TableColumn aColumn)
explicitly states:
该JTable的API进行public void removeColumn(TableColumn aColumn)
明确规定:
Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.
从此 JTable 的列数组中删除 aColumn。注意:此方法不会从模型中删除数据列;它只是删除了负责显示它的 TableColumn。
So the behavior you're experiencing is to be expected. If you're trying to remove data from the model, then you're going to have to change your TableModel's data and ColumnModel. Again for more specific help, you'll need to tell us more.
所以你所经历的行为是可以预料的。如果您尝试从模型中删除数据,那么您将不得不更改 TableModel 的数据和 ColumnModel。再次获得更具体的帮助,您需要告诉我们更多信息。
Consider creating a custom table model and giving it a removeColumn(...)
method that removes all the data from a single column, and then calls the appropriate fireXXX(...)
method.
考虑创建一个自定义表模型并为其提供一个removeColumn(...)
方法,该方法从单个列中删除所有数据,然后调用适当的fireXXX(...)
方法。
Edit
You state in comment:
编辑
您在评论中声明:
THx for answer , I am newbie . This prog is such for studying I have wasted two day for creating it.And now again have problems with it. What is the easiest way ?
THx的答案,我是新手。这个编就是这样的学习,我浪费了两天的时间来创建它。现在又遇到了问题。什么是最简单的方法?
That all depends on what you want to do. If you want to just change the display, then remove the column as you're doing and leave the data alone.
这一切都取决于你想做什么。如果您只想更改显示,请在执行时删除该列并保留数据。
回答by D.Kastier
Based on camickr's solution, I wrote this code to remove
a column from the JTable
.
基于camickr的解决方案,我写了这个代码remove
从一列JTable
。
public class CustomTableModel extends DefaultTableModel {
public void removeColumn(int column) {
// for each row, remove the column
Vector rows = dataVector;
for (Object row : rows) {
((Vector) row).remove(column);
}
// remove the header
columnIdentifiers.remove(column);
// notify
fireTableStructureChanged();
}
}
Note that it does not check if the column can or cannot be removed.
请注意,它不会检查该列是否可以删除。
回答by Vivekanand Rajbhar
jTable1.removeColumn(jTable1.getColumnModel().getColumn(0));
- replace jTable1 with your table name.
- change index number inside "getColumn(0)" according to your table.
- 用您的表名替换 jTable1。
- 根据您的表格更改“getColumn(0)”内的索引号。