In a UI I am trying to get a group of JToggleButtons to display certain corresponding wizard panels in a CardLayout. The problem is that the JToggleButtons are on a JPanel on another JPanel on another JPanel on the parent JFrame. The wizard panels are on a JPanel (with the CardLayout) on a JPanel on a JTabbedPane on the parent JFrame. (i.e. they are related at great-grandparent level...) What's the best way to access the wizard panels from the JToggleButtons and activate their display method? Thanks!
You're going to have to have *some* way to pass a reference to the CardLayout to the JToggleButtons. I'm not sure of the design of your GUI, but there are several ways to do this... If you're basically just building the entire GUI in one class just create the CardLayout early on (or as an attribute of the class ) and use it in both places. If you're using seperate classes for different parts of the GUI, you're going to need to either pass the CardLayout through method parameters all the way to wherever it needs to be used, define the CardLayout statically somewhere (so that all the classes that need it can get to it), or make some kind of static "registry" type hashtable where classes can put references to stuff and other classes can get to them.
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joined: Jun 23, 2003
I am using seperate classes for different parts of the GUI. I started by passing the CardLayout through the chain of classes, but I think it could get messy later. I like the sound of having a static "registry" type hashtable for storing references to be accessed by other classes - do you know of examples or stuff on this? Any help is much appreciated!
Personally, it's probably overkill in a case like this... I would just have one class construct the GUI so that both components would have a local or class level reference to the CardLayout, but maybe there are other considerations here... I don't know the whole picture of your design. Usually I've seen the "static registry" Map used where lots of screens are dynamically generated in some way... but you've got to be careful... since it's static there's a big chance of elements hanging around when they're no longer needed and causing memory leaks. To fix these problems, you'll need to either use weak references or some kind of leasing with a thread that expires leases... anyway, on to the code... a really simple version (that is still vunerable to memory leaks) is: