我有一個JList
的用戶,並且每當列表中的元素被選中時,我將該元素的索引存儲在一個int中。然後,一個JButton
ActionListener
將偵聽按下「刪除用戶」按鈕,並將刪除列表中該元素的用戶。問題是,一旦它最初這樣做,ActionListener
停止運行,所以如果我想刪除另一個元素,該按鈕將不再執行任何操作。我如何確保事件處理程序在它已經完成一次操作後仍能繼續運行?這裏是我的代碼僅供參考:保持一個Java 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();
}
}
}
}
);
我沒有看到您發佈的代碼有任何問題 - 您是否說第二次點擊時刪除按鈕不起作用?你看到控制檯上印有任何異常嗎? – casablanca 2012-04-08 02:03:27
沒有例外。刪除按鈕在第二次點擊時不會做任何事情 – aakbari1024 2012-04-08 02:08:56