I cannot seem to make the Observer pattern work in my case. Here is the problem...I am using separate
Java classes for each JInternalFrame. How do I reference a JInternalFrame which is already open?
This is my code - Frame which I am updating:
// This is a subclass within a JInternalFrame class
// I would like to update this frame and then
// have all updates reflected on
// other open frames.
// Am I referencing the other open frame correctly here? I get no results.
public class AccountBalanceTransaction extends Observable{
public AccountBalanceTransaction() {
allFrames = getDesktopPane().getAllFrames();
for(int z=0; z<=allFrames.length-1; z++){
if (allFrames[z].getName().equals("OtherFrame")){
addObserver((java.util.Observer)allFrames[z]);
}
}
}
public void updateBalance(double bal){
accountBalanceTextField.setValue(new Double(bal));
accountBalanceTextField.repaint();
//notify changes to observers
setChanged();
notifyObservers(bal);
clearChanged();
}
}
WITHIN OtherFrame (implements Observer):
// I just try to reset and repaint the textfield
// obtainCurrentAccountBalance will re-query the database
// to obtain the new balance
public void update(Observable o, Object arg){
currentAccountBalance = obtainCurrentAccountBalance();
accountBalanceTextField.repaint();
}
Thanks for any assistance!