title: How to get the gridlayout cell position of a JButton in Java?
body: <p>I am making a guessing grid game in java using a few buttons. The program uses the java gridlayout layout and when a button is clicked I want it to add its GridLayout position (not x &amp; y) into the array.</p>
  
  <p>Here is a strip of the code where the cell position finder will be needed.</p>
  
  <pre><code>    public void actionPerformed(ActionEvent e)
      {
  
  
        if(placementLimit &lt; 5){
  
         //placementlimit is limit to buttons clicked
  
          JButton clickedButton = (JButton)e.getSource(); 
  
          clickedButton.setBackground(Color.RED);
          /* something like: int pos =  clickedButton.getGridPos
           *  arrayplacedpositions.add(pos);
          */
          placementLimit++;
     } 
  </code></pre>
  
  <p>I did not make a JPanel so
  the whole project is here:</p>
  
  <pre><code>        /* Guessing game GoldMiner By Alexander Smirnov
   * A Computer Game where you hide and place gold.
   */ 
  package standard;
  
  import java.awt.Color;
  import java.awt.Container;
  import java.awt.GridLayout;
  
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  import java.util.ArrayList;
  import java.util.Arrays;
  
  
  import javax.swing.*;
  
  public class game extends JPanel
  {
      JButton buttons[] = new JButton[64];
      int placementLimit = 0;
      ArrayList goldPos = new ArrayList();
  
  
      public game()
      {
          setLayout(new GridLayout(8,8));
          initializebuttons();
      }
  
  
      public void initializebuttons()
      {
          for(int i = 0; i &lt;= 63; i++)
          {
              buttons[i] = new JButton();
              buttons[i].setText("");
              buttons[i].addActionListener(new buttonListener());
  
              add(buttons[i]);
          }
      }
  
  
  
      private class buttonListener implements ActionListener
      {
  
          public void actionPerformed(ActionEvent e)
          {
  
  
              if(placementLimit &lt; 5){
              JButton clickedButton = (JButton)e.getSource(); 
              clickedButton.setIcon(new ImageIcon("/Users/Administrator/Desktop/gold2.jpg"));
              clickedButton.setBackground(Color.RED);
              JPanel.getComponents();
              placementLimit++;
         } else {
  
  
             JOptionPane.showMessageDialog(null, "You have placed the maximum amount of gold!");
  
              } 
  
              }
  
  
  
  
  
  
      }
      public static void main(String[] args)
      {
          JFrame window = new JFrame("GoldMiner");
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.getContentPane().add(new game());
          window.setBounds(300,200,300,300);
          Container c = window.getContentPane();
  
          window.setVisible(true);
      }
  }
  </code></pre>