title: How do I tell which button is being clicked in an array of buttons?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7702697/how-to-get-x-and-y-index-of-element-inside-gridlayout">How to get X and Y index of element inside GridLayout?</a>  </p>
  </blockquote>
  
  
  
  <p>I have an 2D array of buttons that I wish to use. When I want to call an actionListener, how to I tell which button index in this 2D array of mine is being clicked? This is my first time dealing with a listener, so please explain this on a more basic level if you can.</p>
  
  <p>Here is some code of how I have my buttons laid out on a gride (12x12)</p>
  
  <pre><code>//A loop to add a new button to each section of the board grid.
  for (int i = 0; i &lt; gridSize; i++) {
    for (int j = 0; j &lt; gridSize; j++) {
      gameButtons[i][j] = new JButton();
      gameButtons[i][j].setBackground(colors[(int)(Math.random() * numColors)]);
      boardGrid.add(gameButtons[i][j]);
  
      try {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      }
      catch (Exception e) {
      }
  
    }
  }
  </code></pre>
  
  <p>These buttons are randomly assigned a color from an array of colors created earlier. I now have to override actionlistener and I don't know how to do that in a way which allows me to get the button being pressed and compare it to other buttons around it. I would like to mention that I am dealing with static methods.</p>