0

我正在将 JTables 与 mysql 一起使用。在 GUI 中,我有一些 jmenus 和 jbuttons。我有一个带有 ActionListener 的 JMenuItem:

public JMenuBar createMenuBar() {

    bar = new JMenuBar();       
    bar.add(fileMenu);

    add = new JMenuItem("Add Customer");
    fileMenu.add(add);
    add.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (table_HALE_ACCOUNTS == null)
                JOptionPane.showMessageDialog(contentPane, "First Load from Database"); 
            else {
                displayAccountsTable();
                addCustomerContainer();
                contentPane.revalidate();
                contentPane.repaint();
                }   
            }
        });
return bar;
}   

private void displayAccountsTable() {
    if (contentPane != null) {
        contentPane.removeAll();
        contentPane.revalidate();
        contentPane.repaint();
    }
    contentPane = getContentPane();
    contentPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    contentPane.setLayout(new GridBagLayout());
    c = new GridBagConstraints();

    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.CENTER;
    c.weightx = 0.5;
    c.weighty = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;

    contentPane.add(new JScrollPane(table_HALE_ACCOUNTS), c);
    contentPane.revalidate();
    contentPane.repaint();
}


    public void addCustomerContainer() {
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.LINE_END;
    c.weightx = .25;
    c.weighty = 0;
    c.gridx = 1;
    c.gridy = 5;
    c.gridwidth = 1;
    contentPane.add(button_ADD_CUSTOMER, c);

    button_ADD_CUSTOMER.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            String stringDeposit = textField_DEPOSIT.getText();
            double doubleDeposit = Double.parseDouble(stringDeposit);
            String stringDate = textField_DATE.getText().replaceAll("\\s", "");
            int intDate = Integer.parseInt(stringDate);

            try {   
                myAccountTableModel.insertRow(textField_NAME.getText(), textField_CUSTOMER_ID.getText(), doubleDeposit, intDate);
                textFieldSetText();  
                contentPane.revalidate();
                contentPane.repaint();
            } catch (SQLException e1) {
                e1.printStackTrace();
                }               
            }
        }); 
    }

该代码可以很好地显示 JTable 并将一行插入到我的表模型中。问题是当我多次单击 JMenuItem“添加客户”时。这样做并在每次单击 JMenuItem 时执行 ActionEvent insertrow 后点击 JButton“button_ADD_CUSTOMER”。单击 JButton 将插入多行而不是单击单行。

JMenuItem 的 ActionEvent 中的某些内容似乎是通过单击 JButton 来增加和释放的。

我希望防止 JMenuItem 点击的多个插入行,但保留每次我想执行单个插入行时点击 JButton 的能力。

感谢您的时间。

4

0 回答 0