This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JToggleButtons to display a panel elsewhere on the UI

 
Pete Silk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pete Silk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bleeurgh, global variables !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic