确认对话框多次显示

时间:2013-08-24 17:08:53

标签: java swing jtable joptionpane listselectionlistener

我在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!");
                }
            }
        }
    });
}

1 个答案:

答案 0 :(得分:2)

确保未在ListSelectionListener

中调整该值
public void valueChanged(ListSelectionEvent e) {
   if(!e.getValueAdjusting()) {
      ...
   }
}   

参考:How to Write a List Selection Listener