I am implementing a game in java and I am setting up the difficulty settings. I have each difficulty setting in a JMenuItem on a menu bar. I was wondering how do I access and manipulate a variable in another class by using those menu items. Thanks any help is appreciated!
Asked
Active
Viewed 176 times
-1
-
1) Keep a reference to an instance of that class. 2) The class should have a method the action listener can call to change the variable. **BTW** 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Be sure to add relevant tags like the [tag:swing] tag for better visibility to the people most likely to be able to help. – Andrew Thompson Oct 25 '20 at 02:55
1 Answers
-1
The other component should implement the ActionListener
interface. Then add the instance of that other component as action listener:
yourMenutItem.addActionListener(otherComponent);

Stuck
- 11,225
- 11
- 59
- 104