aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JToggleButtons to display a panel elsewhere on the UI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JToggleButtons to display a panel elsewhere on the UI" Watch "JToggleButtons to display a panel elsewhere on the UI" New topic
Author

JToggleButtons to display a panel elsewhere on the UI

Pete Silk
Greenhorn

Joined: Jun 23, 2003
Posts: 9
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!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Pete Silk
Greenhorn

Joined: Jun 23, 2003
Posts: 9
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!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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:

Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Bleeurgh, global variables !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JToggleButtons to display a panel elsewhere on the UI