-1

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!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 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 Answers1

-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