0

我在 addListSelectionListener 中有一个确认对话框。当我在表中选择一行时会触发。然后出现确认对话框,单击“是”或“否”后,它继续出现!

这是我的代码。

public Reference() {
    initComponents();
    fillTable();
    jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            int prompt = JOptionPane.showConfirmDialog(null, "Are you sure you want to Check Out this item?", "Warning", JOptionPane.YES_NO_OPTION);
            if (prompt == 0) {
                String accessNo = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
                String query = "delete from reference where accessNo=" + accessNo + "";
                if (DB.executeNonQuery(query) > 0) {
                    JOptionPane.showMessageDialog(null, "Check out Successfull!");
                    fillTable();
                } else {
                    JOptionPane.showMessageDialog(null, "Check out Failed!");
                }
            }
        }
    });
}
4

1 回答 1

2

确保该值未在ListSelectionListener

public void valueChanged(ListSelectionEvent e) {
   if(!e.getValueAdjusting()) {
      ...
   }
}   

参考:如何编写列表选择侦听器

于 2013-08-24T17:14:24.030 回答