简体   繁体   中英

How to stop actionlistener from repeating previous actions

I'm extremely new to java and coding in general. I'm trying to update information but every time I update more than once it repeats the previous update eg if account 3 is updated and account 4 is updated after, it updates 4 normally and 3 as blanks. How do I make it so that it only updates what I want it to?

updateBtn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
                  menuScreen.setVisible(false);
                  updateScreen.setVisible(true);
                  String id = JOptionPane.showInputDialog("Membership to update (1-4)");

                  if (id.equals("1")) {
                      tf_updateDOB.setText(account1.getAccountDOB());
                      tf_updatePlan.setText(account1.getAccountPlan());
                      tf_updateCard.setText(account1.getAccountCard());
                      tf_updateName.setText(account1.getAccountName());
                      updateSubmitBtn.addActionListener(new ActionListener() {
                          public void actionPerformed(ActionEvent e) {
                              account1.setAccountDOB(tf_updateDOB.getText());
                              account1.setAccountCard(tf_updateCard.getText());
                              account1.setAccountPlan(tf_updatePlan.getText());
                              account1.setAccountName(tf_updateName.getText());
                              menuScreen.setVisible(true);
                              updateScreen.setVisible(false);
                          }
                      });

                  } else if (id.equals("2")) {
                      tf_updateDOB.setText(account2.getAccountDOB());
                      tf_updatePlan.setText(account2.getAccountPlan());
                      tf_updateCard.setText(account2.getAccountCard());
                      tf_updateName.setText(account2.getAccountName());
                      updateSubmitBtn.addActionListener(new ActionListener() {
                          public void actionPerformed(ActionEvent f) {
                              account2.setAccountDOB(tf_updateDOB.getText());
                              account2.setAccountCard(tf_updateCard.getText());
                              account2.setAccountPlan(tf_updatePlan.getText());
                              account2.setAccountName(tf_updateName.getText());
                              menuScreen.setVisible(true);
                              updateScreen.setVisible(false);
                          }
                      });
                  }

Welcome to SO. I suspect your problem is that you are using addActionListener so that each time the code is run another listener is added. All the previous ones you have added run as well when the button is clicked.

Much better would be to have a single listener added that references whichever account was last chosen:

private class AccountUpdater implements ActionListener {
    private Account account;

    public void setAccount(Account account) {
        this.account = account;
        tf_updateDOB.setText(account.getAccountDOB());
        tf_updatePlan.setText(account.getAccountPlan());
        tf_updateCard.setText(account.getAccountCard());
        tf_updateName.setText(account.getAccountName());
    }

    @Override
    public void actionPerformed(ActionEvent f) {
        account.setAccountDOB(tf_updateDOB.getText());
        account.setAccountCard(tf_updateCard.getText());
        account.setAccountPlan(tf_updatePlan.getText());
        account.setAccountName(tf_updateName.getText());
        menuScreen.setVisible(true);                                                                                                   
        updateScreen.setVisible(false);                        
    }
}

AccountUpdater updater = new AccountUpdater();
updateSubmitBtn.addActionListener(updater);        

Then in your code following the option dialog, you just need to do:

if (id.equals("1")) {
    updater.setAccount(account1);
} else if (id.equals("2")) {
    updater.setAccount(account2);
} ...

This will also make your code much easier to read with less repetition.

Even better would be if the accounts have an id field:

List<Account> accounts = ...;
accounts.stream()
    .filter(ac -> ac.getId().equals(id))
    .findAny().ifPresent(updater::setAccount);

Then you don't need the if , else if statements (which are often a sign of poor design).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM