Java Reference
In-Depth Information
listeners.clear();
}
}
Next, change your view to behave as a listener. You also need to modify the view to accept listening
controllers. In most cases, every view will have a single listening controller, so you can just keep this
as a field instead of a list. Also, the controller object is used directly instead of defined as a separate
listening interface (you can try to define an AccountViewListener and work from there if you
want).
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class AccountWindow extends JFrame implements AccountListener,
ActionListener {
private JTextField funds, add;
private JButton addButton;
private AccountController controller;
public AccountWindow() {
this.setSize(400, 120);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
funds = new JTextField(30);
funds.setEditable(false);
add = new JTextField(30);
addButton = new JButton("Add funds");
addButton.addActionListener(this);
this.add(funds);
this.add(add);
this.add(addButton);
}
@Override
public void notifyFundsChanged(double newAmount) {
this.funds.setText("Your funds: "+newAmount);
this.add.setText("");
}
public void registerController(AccountController controller) {
this.controller = controller;
}
@Override
public void actionPerformed(ActionEvent e) {
Search WWH ::




Custom Search