我正在为包含来自数据库的搜索结果的 JTable 编写代码,我需要通过 setRowCount(0) 方法或 initTable() 方法在每次单击“doSort”按钮时将其初始化为没有行,我已经编写了以下代码,但它不起作用:
private void doSortActionPerformed(java.awt.event.ActionEvent evt) {
try {
ArrayList<String> all = null;
String resStr = null;
mail = new Mail();
tm.setRowCount(0);
initTable(tm);
if(incomSelect.isSelected())
try {
all = mail.select("SELECT * FROM mails WHERE type = 'incoming'");
} catch (SQLException ex) {
}
else if(outcomSelect.isSelected())
try {
all = mail.select("SELECT * FROM mails WHERE type = 'outcoming'");
} catch (SQLException ex) {
}
for(int i=0; i<all.size(); i++) {
resStr = all.get(i);
String[] resArr = resStr.split(",");
tm.insertRow(i, resArr);
}
} catch (SQLException ex) {
}
}
和 initTable() 代码:
public void initTable(DefaultTableModel mod){
for (int i = mod.getRowCount()-1; i >= 0; i--) {
mod.removeRow(i);
}
}