有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java确认对话框多次显示

我在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) 个答案

  1. # 1 楼答案

    确保值未在ListSelectionListener中调整

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

    参考:How to Write a List Selection Listener