The environment: I have three classes A, B and C. Each is defined in separate .java files. They all extend JPanel. A1 is my Public class. A1 sets up a JTabbedPane. A1 adds B1 and C1 as separate tabs in A1's JTabbedPane.
The problem: Periodically B1 needs to add a row to a JTable on C1.
The Question: How can B1 address a method that update the JTable in C1?
Joined: Jun 22, 2009
I got passed the initial issue, but it required me to set the method in C1 to static. Why?
I guess you can close this.
Morrie Segal wrote:I'm not sure how to fix this. I assume that an instance context is referring to an objet instance. What does a static context refer to?
Thanks, for taking the time to educate me on this nuance.
A static context means you are calling a method on a class but don't have/need a reference to an instance of that class.
If you wanted to keep the method an instance method (non-static) then you would need to pass a reference to C1 into B1.
So you said you now do this:
1) You have an Object A1 which makes a tabbed pane
2) A1 makes an Object named B1 and adds it to the tabbed pane
3) A1 makes an Object named C1 and adds it to the tabbed pane
You would need to add:
4) A1 passes the reference to C1 into B1
B1 would have a method which takes a reference to C as a parameter and stores it. Then when B1 needs to update the table it calls the C object's method.
An alternative is to isolate the data (called a Model) from C and pass the Model to both B1 and C1. B1 updates the Model and C1 reacts to updates. This helps keep B1 from being dependent on C1 (the less tightly coupled your GUI components are the better). See this tutorial and the TableModel JavaDocs for how to use.
Joined: Jun 22, 2009
I think I get it. I missed the point that there may be multiple C1 instances. The compiler does not know which one I am addressing. The update from B1 could have one of two contexts:
1. Update a variable in a specific instance of C1 (instance).
2. Update a variable that is shared between all C1s (static).
In the first version, B1 needs to know which C1 to address. In the second version I need to define the method and target variable as static so that all C1 instances see the update.
The model concept provides an abstraction layer between the target variable and both the B1 and C1 instances. I'l have to try that next.
Thanks for taking the time to teach.
subject: Calling a method in object outside of your scope