好的,所以我试图从我的数组中删除一个对象,每个对象都有一个用户 ID,所以数组中的 0 索引将保存一个 ID 为 1 的对象。例如,当我删除 id 为 2 的对象时,我想删除数组中的 1 索引并重做模型并显示它。所以我希望模型显示 1:blah 然后 3:Blah 等等,忽略已删除的
private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
// arrays index not working when deleted
int reply = JOptionPane.showConfirmDialog(null, "Are you sure?","Delete Patient: " +
NameTextField.getText(), JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
int Num = Integer.parseInt(IDTextField.getText());
int Num1 = Num--;
for(Patient s: PatientList)
{
if(Num == s.getAccountNumber())
{
PatientList.remove(Num1);
break;
}
}
DefaultListModel PatientListModel = new DefaultListModel();
for(Patient s: PatientList)
{
PatientListModel.addElement(s.getAccountNumber() + "-" + s.getName());
}
PatientJList.setModel(PatientListModel);
//delete procedures for that id
JOptionPane.showMessageDialog(null, "Patient: " + NameTextField.getText() + "
Deleted!");
NameTextField.setText("");
AgeTextField.setText("");
AddressTextField.setText("");
SexGroup.clearSelection();
PhoneTextField.setText("");
IDTextField.setText("");
PatientJList.clearSelection();
}
else {
}
}