• Post Reply Bookmark Topic Watch Topic
  • New Topic

Repainting JInternal Frames

 
Josh Juneau
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am relatively new to Swing..and I must say that it is great once you get the hang of it. I have found that many implementations seem to be rather obscure and take a bit of practice to implement. I have managed to create an application which is composed of a JDesktopPane...hosting many JInternalFrames within.

My question: How can I invoke an action within one JInternalFrame and cause repainting of other JInternalFrames?

For instance, I have an application which is used for account management and I would like to use an UPDATE button within one JInternalFrame to update an amount. This amount should now be reflected within all other JInternalFrames which also contain that value.

I know this is possible...but I just cannot seem to implement the solution.

Thanks for any assistance!

Josh
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know the details of your design so this may not work but I'd try the observer pattern. Usually when I have something that needs to be displayed in a variety of places I'll have each object that needs to use it register as an observer with the object that contains/controls it. Whenever it gets changed the observers are notified with the new information or are expected to get the information themselves.
 
Josh Juneau
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try out the observer pattern. I have just entered it into Google and retrieved many hits. I do not know how I have never heard of this pattern before. Is there a JFC Swing reference which contains everything (I know that is a lot)? I currently have the JFC Swing Guide to Creating A GUI reference.

Thanks for your assistance. Hopefully I can make observer work for my application, it sounds like it should do the trick.

Josh
 
Josh Juneau
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all I've never actually used the Observable class or the Observer interface provided in java.util. When I have used the observer pattern it's been with my own interfaces and in different forms depending on the situation. For example, I had information that needed to be used in a variety of ways by different classes and could be changed by any of them in which case the others needed to refresh their data.

The code might look something like this:

1) Observable Interface


2) Observer Interface


3) Each Frame


4) Class with the Balance


As for how you add the frame as an observer, where that code goes is really based upon your design. The truth is you don't even NEED the Observable interface depending on your requirements. You could simply use the Observer interface for each frame that needs to be notified and provide the methods for adding/removing/notifying them in the AccountBalanceTransaction class.
[ December 14, 2004: Message edited by: Ken Blair ]
 
Josh Juneau
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response, Ken.

I am going to try and write my own Observer such as your explaination describes.

I will post another reply and let you know how this pans out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!