Gui
Gui

Reputation: 65

JButton repeating action depending on how many times pressed

I'm building a simple program in Java which gets a balance (mine is set to $8000). I want to deposit money into that so I have a UI with 0-9 buttons, a textarea and a deposit button, so if the user wanted to deposit $100 he would press 1 once then 0 twice. All that works and it deposits for the first time, but the second time it deposits the double amount of money. If I press my deposit button 10 times and select $1 then press enter it deposits $10. I think the structure of my btn action listener might be wrong.

Any ideas?

Code:

btnDeposit.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        label1.setText("Deposit: How much would you like to deposit?");
        btnWithdraw.setEnabled(false);
        btnBalance.setEnabled(false);

        btnEnter.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                //convert text area into double
                double depositNumber = Double.parseDouble(ta.getText());
                sav.deposit(depositNumber);
                btnWithdraw.setEnabled(true);
                btnBalance.setEnabled(true);
            }

        });

    }
});

My deposit function is:

public void deposit(double depositAmount) {
    balance += depositAmount;
    System.out.println("Your updated balance is: " + balance);
}

I also noticed that it doesn't go back to where it started, if I keep clicking on Enter it keeps adding and adding...

Button that clear my text area:

btnClear.addActionListener(new new ActionListener(){ 
@Override
public void actionPerformed(ActionEvent ae) {
  ta.setText("");
}
});

Upvotes: 0

Views: 1236

Answers (1)

Murat Karagöz
Murat Karagöz

Reputation: 37594

The problem is called out in the comment section. You are declaring multiple listeners which are calling the respective deposit() or withdraw() method every time you perform an action.

To avoid this. You can set one listener class to all of your buttons like this.

Create an inner class

private class MySpecialListener implements ActionListener{

@Override
            public void actionPerformed(ActionEvent ae) {
               if(e.getSource == btnDesposit) // do stuff and so on
            }

}

and add the listener like this

MySpecialListener myListener = new MySpecialListener();
btnDeposit.addActionListener(myListener);

The above requires you to re-write your code, but it has a better structure then your current one. To fix your current problem you can remove the last listener like this:

for(ActionListener al : btnEnter.getActionListeners())
 btnEnter.removeActionListener(al)

btnEnter.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            //convert text area into double
            double depositNumber = Double.parseDouble(ta.getText());
            sav.deposit(depositNumber);
            btnWithdraw.setEnabled(true);
            btnBalance.setEnabled(true);
        }

    });

Upvotes: 1

Related Questions