2

I am making a maze program in Java which consists of a grid of MazeButtons which extend JButton and have a field for State (which is the location of the button and some other information about how the maze should work). Another class, MazeFrame, extends JFrame and implements ActionListener. When I construct the GUI in the setup class, I add the MazeFrame ActionListener to each button. I want the actionPerformed method in MazeFrame to be able to check to see if the action the user attempted is allowed, but in order to do that I need to know which button was clicked.

How can you know which button called a given actionPerformed method?

Chris
  • 44,602
  • 16
  • 137
  • 156
dylanrb123
  • 61
  • 1
  • 6

1 Answers1

3

Use the getSource method on the event and it will return the object that fired it

Johm Don
  • 609
  • 2
  • 5
  • 15