我正在为包含来自数据库的搜索结果的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);
}
}