I'm developing an application with Swing and had some questions about the correct way to use actions/listeners in multiple files. I have a main GUI class with a menu bar/menu items and each menu item has an action. The actions are in their own classes. I also have a class that extends JInternalFrame to use with the application's desktop pane. When a JInternalFrame is activated, I want the main GUI's menu bar->boldCheckBoxMenuItem's enabled property to be updated based on the state of the internal frame's text area. Code attached below.
I know some of these topics have been discussed in the forums, but I'm having trouble figuring out how to adapt the information for my application.
First question: What is the ideal way to let actions access the main GUI? Should you pass the GUI instance as a parameter in the action's constructor?
Second question: When an internal frame is activated, how do I access a menu item from the main GUI's menu bar? Should you pass the GUI instance as a parameter in the frame's constructor?
Thanks for your help. If you need any more information, please let me know.
I didn't read your code; I do have some comments on your questions.
If I understand this correctly, there is a 'main' JFrame and a 'subordinate' JFrame or JDialog, and you have things in the former that depend on the latter.
At least consider defining an interface with methods that update things in the main; then pass an instance of a class that implements that interface to the subordinate frame. At the proper time, the subordinate calls methods to do updating. It does not have to be calling the main class (though it could be), does not have to know what the update does, does not have to know that menus or whatever are getting affected by the updates, etc. This way, you are encapsulating the job you want done in that interface, and hiding the implementation details from the caller. This makes your two UIs less dependent on each other, makes it easier to change implementations, and most importantly gives you an overall mechanism for updating things going in this direction. It also localizes the updating getting done to those methods, in case you want to track them or find a bug in them.