Reputation: 1264
I'm trying to figure out how I can have a class listen to another. So this is the idea.
I have a MainFrame
class, which is simply a container class, JFrame container, that takes an argument of type JPanel. Basically I want this container class to be able to switch between frames depending on what my other class, FrameSwitcher
, will tell it to do.
The other classes are: FrameSwitcher, MainMenu and ScoreBoards
.
The idea is that, let's say MainMenu
, will contain 4 buttons, each one will listen, BUT will NOT change the frames. Rather it will somehow - and this is the part I need help with - send to the FrameSwitcher
what button was clicked, and this information will then be sent to MainFrame
to switch to the appropriate frames.
Upvotes: 0
Views: 831
Reputation: 205785
You may be looking for the observer pattern, discussed here. In particular, a PropertyChangeListener
, illustrated here, may be a useful approach to loose coupling.
Also consider letting each view export an Action
that selects itself from a CardLayout
, as suggested in How to Use Actions and How to Use CardLayout.
Upvotes: 3
Reputation: 57381
FrameSwitcher
should keep ActionListeners
added to the menu. On click it changes it's state and call MainFrame's method switchTo(argumentWhereToSwitch);
Upvotes: 2