我有一个JList
用户,并且只要选择列表中的元素时,我将在int中存储该元素的索引。然后 aJButton
ActionListener
将侦听“删除用户”按钮的按下,并将删除列表中该元素处的用户。问题是,一旦它最初这样做,ActionListener
停止运行,所以如果我想删除另一个元素,按钮将不再做任何事情。我如何确保事件处理程序即使在它已经完成一次之后也能继续运行?这是我的代码供参考:
/*
* Listener for user list selection
*/
userList.addListSelectionListener(
new ListSelectionListener () {
public void valueChanged(ListSelectionEvent e) {
delete.setEnabled(true);
index = userList.getSelectedIndex();
}
}
);
/*
* Listener for delete button press
*/
delete.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = JOptionPane.showConfirmDialog(null,
"Are you sure you want to delete user " + users.get(index) + "?");
switch(i) {
case JOptionPane.YES_OPTION:
try {
Controller.deleteUser(users.get(index));
users.remove(index);
listModel.removeElementAt(index);
userList = new JList(listModel);
}
catch (UserNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
);