How to reference JTabbedPane from another class?
Hi,
I'm creating an application with a JTabbedPane with 3 tabs.
The JTabbedPane is defined in the "mainClass".
Each tab is a different class (tab1 = Class1, tab2 = Class2 and tab3 = Class3).
Each Class extends JPanel (not the "mainClass").
There is a table in tab1, when an item of that table is selected, I want the tab2 to be displayed.
The method to do that is (to my understanding) tabbedPane.setSelectedIndex(int).
But how do I reference the tabbedPane define in "mainClass" when the action of selecting an item is handled in Class1?
Hope I'm clear enough.
Thanks
Does anyone have an answer for this question?
First, you have a scope problem. Identify the problem first, then simplify. The second class probably needs a method to select the GUI widget and using an integer argument as the key. You would create a Listener on the Table,tree,list, etc. and then as the event is fired, you figure out what integer or key you need and then call the "PUBLIC" method on the second class passing the integer key as the argument.
Also, if you create an Interface e.g.
Interface myTabConst {
public static final int TAB_CAT = 1;
public static final int TAB_DOG = 2;
}
You can then use things like ...
switch(some integer) {
case myTabConst.TAB_CAT:
// do something
break;
case myTabConst.TAB_DOG:
// do something else
break;
Now you just keep the Interface class on hand while you are coding and also, the terms are more understandable.
Let me know if this helps.
Pete,
Thanks for the answer.
But it's the scope issue that I am not able to figure out. An instance of Class1 does not know of an instance of Class 2 or Class3 or of MainClass. (except through many calls to getParent() as the actual class hiarchy is much deeper)
If I have MainClass which contains TabbedClass which has tabs Class1, 2, and 3. I want to do something in Class1 tresult in InstanceOfTabbedClass.setSelectedIndex(Index for InstanceOfClass2) But InstanceOfTabbedClass is out of scope (except through getParent of InstanceOfClass2.
I got around this by defining a static instance of TabbedClass and a final instance of TabbedClass with in MainClass and than in MainClass's main() I create an instance of MainClass and than assign finalInstanceOfTabbedClass to staticInstanceOfTabbedClass.
staticInstanceOfTabbedClass = InstanceOfMainClass.finalInstanceOfTabbedClass;
Thus staticInstanceOfTabbedClass is available globaly as InstanceOfTabbedClass.staticInstanceOfTabbedClass. But this doesn't seem like such a good idea because if finalInstanceOfTabbedClass gets garbage collected I am not sure that staticInstanceOfTabbedClass would.
Your additional comments would be greatly appreciated.
thanks again
woops....in the forth paragraph InstanceOfTabbedClass.staticInstanceOfTabbedClass should be InstanceOfMainClass.staticInstanceOfTabbedClass
Wow, how about a copy of the code and point out the line that doesn't work. I can probably figure out your problem once I see the code.Thanks,PiratePete
Thanks but this particular app is about 25,000 lines. I need to trim it down to a good example first. I hope to get to that done next week.
I will say of experience (19 years) that if your class or method is that large, that you are doing something wrong. If you break your units down into black-box methods, debug, and test, you will fair much better. Please do post your code when you can, I'll continue to watch this item.
Software Process is an evolving one. You should continue to design, analyze, code, and test. Many times problems like these are best solved when I can simmplify the whole process. A sequence diagram always helps.
PiratePete
24,000 lines, 50+ classes, many more methods.Probably a little weak on the black box test for each class or method, I'll admit.I shouldn't have implied that all 24,000 lines were involved.
please see http://forum.java.sun.com/thread.jsp?forum=57&thread=366900