I have two panels in my JFrame.. One, the left one, which contains buttons for navigation. I want to control the CardLayout of the right one using buttons in the left one. I tried "fetching" it to the JFrame and passing it through the constructor of the left one but it produced some weird effects.
How do I do this?
You will need to pass both the CardLayout
AND a reference to the right panel to the left panel.
Personally, I would create a "navigation controller" which would contain the CardLayout
and right panel, this I would pass to the left panel. The left panel then would be able to "ask" the "navigation controller" to move in a given direction, based on what the "navigation controller" was capable of. This way, you limit the exposure of the both the CardLayout
and right panel, but it also allows you to change the mechanisms by which the navigation actually works without adversely effecting the any class that might be working with the "navigation controller"