So the Main program gets a ledger, and that ledger contains the accounts
with the entries that they all want to read, right ?
Then Main and ledger and all other interested parties need (at least) a method like:
Code:
public void accountEntrySelected(AccountEntry acc) {
.
textFieldForValue.setText(Integer.toString(acc.getValue()));
}
that any present Account object would call when an entry was selected or
changed.
The good way to do this is to make an interface
Code:
public interface AccountListener {
public void accountEntrySelected(AccountEntry acc);
public void accountEntryChanged(AccountEnrty acc);
}
Then you define the Main_Program and the Ledger as implementations of this
interface like
Code:
public class Main_Program implements AccountListener {
.
.
public void accountSelected (AccountEntry) {
textFieldForValue.setText("selected:"+Integer.toString(acc.getValue()));
getValue()));
}
public void accountEntryChanged(AccountEnrty acc) {
textFieldForChangedValue.setText("changed:"+Integer.toString(acc.
}
Add a new variable
private ArrayList accountListeners=new ArrayList();
to Account.
Then add three methods to Account:
Code:
public void addAccountListener (AccountListener al) {
accountListeners.add(al);
}
public void broadcastChange (AccountEntry ae) {
for (int i=0; i<accountListeners.size(); i++) {
(AccountListener) al=(AccountListener)accountListeners.get(i);
ai.accountChanged(ae);
}
}
public void broadcastSelection (AccountEntry ae) {
for (int i=0; i<accountListeners.size(); i++) {
(AccountListener) al=(AccountListener)accountListeners.get(i);
ai.accountSelected(ae);
}
}
To sum it up then; when a new Account is opened in Ledger you do like:
account.addAccountListener(this);
account.addAccountListener(this.mainProgram);
(I assume the ledger has a pointer to a Main_program instance).
Then you place calls to the broadcast methods inside Account wherever it is
required.
mkay ?