本文介绍了删除JTable中的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要删除JTable中的所有行。
I need to remove all the rows in my JTable.
我尝试了以下两种方法:
I have tried both of the following:
/**
* Removes all the rows in the table
*/
public void clearTable()
{
DefaultTableModel dm = (DefaultTableModel) getModel();
dm.getDataVector().removeAllElements();
revalidate();
}
和
((DefaultTableModel)table.getModel()).setNumRows(0);
这两项都不会删除所有行。有什么想法吗?
Neither of which would remove all the rows. Any ideas?
推荐答案
以下代码对我有用:
DefaultTableModel dm = (DefaultTableModel) getModel();
int rowCount = dm.getRowCount();
//Remove rows one by one from the end of the table
for (int i = rowCount - 1; i >= 0; i--) {
dm.removeRow(i);
}
这篇关于删除JTable中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!