Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable Scope Problem

 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application which is split into 3 distinct panels. These panels are used for displaying app navigation, user messages and the last panel is to display various panels (card layout) which are the foundation of the application. My problem is as follows.

The panels connect to a SQL database for updating parts information. I would like to be able to display messages in the message panel in accordance to what is being performed (error messages, success messages, etc). I have a method in the main form class. In this main form, I have declared and instantiated all the classes (which are the panels themselves). What would be a common means to get access to this method? I'm totally stumped by this one because the variable for the main class just isn't in the scope of the panels. ???

Help please.

Thanks,
David
 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way I can see to do it is to create a threaded server socket and just have a listener class for a particular port in the main class. Then allow other classes to send messages through that port number. I don't like this method, but it would work I suppose.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I'm working on the same idea in my program.

I just want to understand exacly what you need first...

Am I understanding correctly that you have one main class (a frame?) that instantiates the three panels. And on one of these panels, there is information generated that must be displayed on another panel?

If this is the case, you could pass the instance of the main class into the three panels. Then when you need to, call a method on the main class that would in turn call a method on the required panel's class.

Does that make sense for you?

Rachel
 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel,

Thanks for the quick reply. That's what I'll try right now. I was just sitting here coding in a simple threaded message server to it and your solution is about 100X easier. Thanks a bunch.

David
 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worked like a charm. Funny how you can make rather simple solutions complex!!

Thanks again.
 
Andrei Socaciu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Actually the "correct" solution would be to use the Observer pattern - see Observer and Observable in the JDK.
This way you avoid the circular dependence (Main knows about the panels and the panels know about Main).
It works like this. Suppose Panel1 generates data that you want to have in Panel2
then you need to make Panel1 an Observable class. Since you already inherit from JPanel you cannot inherit from Observable so
you need to use composition.
public class Panel1 extends JPanel {
private Observable observable = new Observable();
...
void addObserver(Observer o) {
observable.addObserver(o);
}

//when loading the data from the db
Data data = getDataFromDB();
observable.setChanged();
observable.notifyObservers(data);
observable.clearChanged();
}

then your Panel2 needs to implement the Observer interface
public class Panel2 extends JPanel implements Observer {
public void update(Observable o, Object arg){
//display the changed data
}

}

then in main, you register Panel2 as an observer to Panel1
...
panel1.addObserver(panel2);
...

Cheers,
Andrei
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic