Source of BankAccountView.java


  1: //BankAccountView.java
  2: //Adapted from an example by Tony Sintes.
  3: //BankAccountView is responsible for constructing the GUI 
  4: //and creating the associated controller.

  6: import javax.swing.JPanel;
  7: import javax.swing.JButton;
  8: import javax.swing.JTextField;
  9: import javax.swing.JLabel;
 10: import java.awt.BorderLayout;
 11: import java.util.Observable;
 12: import java.util.Observer;

 14: public class BankAccountView extends JPanel
 15: implements Observer
 16: {
 17:     private BankAccountModel model;
 18:     private BankAccountController controller;
 19:     private JButton depositButton  = new JButton("Deposit");
 20:     private JButton withdrawButton = new JButton("Withdraw");
 21:     private JTextField amountField = new JTextField();
 22:     private JLabel balanceLabel    = new JLabel();

 24:     //Constructor
 25:     public BankAccountView(BankAccountModel model)
 26:     {
 27:         this.model = model;
 28:         this.model.addObserver(this);
 29:         this.controller = new BankAccountController(this, model);
 30:         depositButton.addActionListener(controller);
 31:         withdrawButton.addActionListener(controller);
 32:         setLayout(new BorderLayout());
 33:         JPanel buttons = new JPanel(new BorderLayout());
 34:         JPanel balance = new JPanel(new BorderLayout());
 35:         buttons.add(depositButton, BorderLayout.WEST);
 36:         buttons.add(withdrawButton, BorderLayout.EAST);
 37:         balance.add(balanceLabel, BorderLayout.NORTH);
 38:         balance.add(amountField, BorderLayout.SOUTH);
 39:         add(balance, BorderLayout.NORTH);
 40:         add(buttons, BorderLayout.SOUTH);
 41:     }

 43:     //Called by model when the model changes
 44:     public void update(Observable thing, Object obj)
 45:     {
 46:         balanceLabel.setText("Balance: " + model.getBalance());
 47:     }

 49:     //Provides access to the amount entered into the field
 50:     public double getAmount()
 51:     {
 52:         //Assume that the user entered a valid number
 53:         return Double.parseDouble(amountField.getText());
 54:     }
 55: }