我有一個JTable,它是一個DefaultTableModel。表中的數據是從ArrayList CarList訪問的。我遇到的問題是,在刪除行後,它只是暫時刪除。要刪除項目中的行,用戶必須選擇一行,然後按下按鈕刪除。當我使用我正在使用的編碼時,該行將從jTable中刪除,但數據不會從我的Arraylist中刪除,因此當我再次打開JTable時,我刪除的行仍然存在。誰能幫幫我嗎?我有一些編碼這裏:從Jtable刪除行後保存
ArrayList CarList = CarRentalSystem.CarList;
UsingFiles CarFile = CarRentalSystem.CarFile; //ArrayLists accessed from the whole project
/**
* Creates new form ViewCars
*/
public ViewCars() { //creating the table and accessing the data from the arraylists
initComponents();
this.CarList=CarList;
DefaultTableModel model = (DefaultTableModel) carTable.getModel();
for (int i=0; i<CarList.size(); i++){
Car car = (Car) CarList.get(i);
model.addRow(new Object[]{car.getCarNum(), car.getManufacturer(), car.getModel(), car.getBooked(), car.getAircondition()});
btnEdit.setVisible(true);
btnDelete.setVisible(false);
btnSave.setVisible(false);
}
//刪除按鈕編碼
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) this.carTable.getModel();
int[] rows = carTable.getSelectedRows();
for(int i=0;i<rows.length;i++){
model.removeRow(rows[i]-i);
JOptionPane.showMessageDialog(null, "Row Deleted"); //the row is deleted but the data isn't
}
}
}