Question

I'm having problems with my deposit and withdraw buttons. They don't do anything when I click on them.
My goal is to have the user deposit and withdraw from their account.
Sorry I'm new to this GUI thing.

bankAccount file with the deposit and withdraw calculations

public class bankAccount {  
    private double balance;

    public bankAccount() {   
        balance = 0;
    }

    public bankAccount(double initialBalance) {   
        balance = initialBalance;
    }

    public void deposit(double amount) {  
        double newBalance = balance + amount;
        balance = newBalance;
    }

    public void withdraw(double amount) {   
        double newBalance = balance - amount;
        balance = newBalance;
    }

    public double getBalance() {   
        return balance;
    }
}



I'm having trouble in this file at the actionPerformed method.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class AccountPanel extends JPanel implements ActionListener {  
    private JLabel amountLabel, resultLabel;
    private JTextField amountTextField;   
    private JButton depositButton, withdrawButton;
    private bankAccount account;
    double result;

    public AccountPanel() {
        JPanel displayPanel = new JPanel(); 
        displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        amountLabel = new JLabel("Amount:"); 
        displayPanel.add(amountLabel);

        amountTextField = new JTextField(13); 
        displayPanel.add(amountTextField); 

        JPanel resultPanel = new JPanel(); 
        resultPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        resultLabel = new JLabel("Balance = "); 
        resultPanel.add(resultLabel);

        //buttons
        JPanel buttonPanel = new JPanel(); 
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        // deposit button 
        depositButton = new JButton("Deposit"); 
        buttonPanel.add(depositButton); 

        // withdraw
        withdrawButton = new JButton("Withdraw"); 
        buttonPanel.add(withdrawButton); 

        // add panels to main panel 
        this.setLayout(new BorderLayout()); 
        this.add(displayPanel, BorderLayout.WEST);
        this.add(resultPanel, BorderLayout.SOUTH); 
        this.add(buttonPanel, BorderLayout.EAST); 
    }



    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource(); 
        if (source == depositButton) {
            double dp = Double.parseDouble(amountTextField.getText());
            double dpamount = account.getBalance() + dp;
            account.deposit(dpamount);
            result = dpamount;
            resultLabel.setText("" + result);
            depositButton.addActionListener(this); 
        }
        else if (source == withdrawButton) { 
            double wd = Double.parseDouble(amountTextField.getText());
            account.withdraw(wd);
            resultLabel.setText("" + result);
            withdrawButton.addActionListener(this); 
        } 
   }
}



import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JPanel;
class AccountFrame extends JFrame { 
    private static final int FRAME_WIDTH = 400;
    private static final int FRAME_HEIGHT = 100;

    public AccountFrame() { 
        setTitle("Bank Account"); 
        setSize(FRAME_WIDTH, FRAME_HEIGHT); 
        centerWindow(this); 
        setResizable(false); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        JPanel panel = new AccountPanel(); 
        this.add(panel); 
    } 

    private void centerWindow(Window w) { 
        Toolkit tk = Toolkit.getDefaultToolkit(); 
        Dimension d = tk.getScreenSize(); 
        setLocation((d.width-w.getWidth())/2, 
        (d.height-w.getHeight())/2); 
    } 
}



import javax.swing.*; 
public class Account{ 
    public static void main(String[] args) { 
        JFrame frame = new AccountFrame(); 
        frame.setVisible(true); 
    } 
} 
Was it helpful?

Solution

Try doing something like this instead of using a single actionPerformed() method:

Register event handler first (you missed this step):

  depositButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
       handleDepositButtonEvent(evt);
     }
  });

Implement custom event handler for that event:

  private void handleDepositButtonEvent(ActionEvent evt){
     double dp = Double.parseDouble(amountTextField.getText());
     double dpamount = account.getBalance() + dp;
     account.deposit(dpamount);
     result = dpamount;
     resultLabel.setText("" + result);
  }

Do that for each button and component generating events you want to capture; separate action methods are easier to read and maintain.

By the way, don't add the action listener in the action listener! This makes no sense, and it won't work anyway.

if (source == depositButton) {
    double dp = Double.parseDouble(amountTextField.getText());
    ...
    depositButton.addActionListener(this); //<--- don't do this
} 

OTHER TIPS

You've not registered any ActionListeners to either the depositButton or withdrawButton.

Before you tell me that they are registered in the actionPerformed method, you must realise that nothing is calling this method and you shouldn't really be registering your ActionListener here, or at least not for the reasons you are anyway, they should be added within the constructor so that it is registered when the program runs

Take a closer look at How to Write an Action Listener and How to Use Buttons, Check Boxes, and Radio Buttons for more details

The depositButton.addActionListener(this) and withdrawButton.addActionListener(this) calls need to be in the AccountPanel constructor. Right now they'll never get executed, because the buttons have no event handlers initially.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top