I'm practicing my gui skills with Java and I have been doing menus and menu bars. They make sense to me just fine but I have a question about where I can learn more about them.
The basic menus I have done, the ActionListener function actionPerformed has to be in the same class as the menu, and the item that the menu is changing also has to be in the class as the menu.
What If I want to have a menu that affects a JPanel that is created by a constructor from another class and placed in my frame.. I'm not sure how the menu can change components of it.
Any tips, hints or sites you guys have found helpful would be great, thanks in advance.
I find it useful to wrap menubar actions in an
Actionobject. This encapsulates:You would define a subclass of
AbstractActionin the class whose data that action acts on. Then define a getter for that action so that your menu building code can get it. You can add theActiondirectly to a menu - aMenuItemis constructed automatically for it.The other advantage of actions is that that same action can be used in a button, toolbar, etc.