This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Variable Scope Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Variable Scope Problem" Watch "Variable Scope Problem" New topic

Variable Scope Problem

David Mace
Ranch Hand

Joined: May 26, 2004
Posts: 35
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.


Dad always thought laughter was the best medicine, which I guess is why several of us died of tuberculosis. -Jack Handy
David Mace
Ranch Hand

Joined: May 26, 2004
Posts: 35
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

Joined: May 18, 2004
Posts: 434
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?

David Mace
Ranch Hand

Joined: May 26, 2004
Posts: 35

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 Mace
Ranch Hand

Joined: May 26, 2004
Posts: 35
Worked like a charm. Funny how you can make rather simple solutions complex!!

Thanks again.
Andrei Socaciu

Joined: Jun 28, 2004
Posts: 5
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) {

//when loading the data from the db
Data data = getDataFromDB();

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

It is sorta covered in the JavaRanch Style Guide.
subject: Variable Scope Problem
Similar Threads
Adding Panels in Frame or in a Panel
More MVC......question for Mark
Using Flyweight with Panels
New ground, making progress, but need some guidance with drawing lines.
Why doesn't a component in a Listbox see mouse clicks when using a DefaultListCellRenderer ?