0

Possible Duplicate:
How to get X and Y index of element inside GridLayout?

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.

Here is some code of how I have my buttons laid out on a gride (12x12)

//A loop to add a new button to each section of the board grid.
for (int i = 0; i < gridSize; i++) {
  for (int j = 0; j < 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) {
    }

  }
}

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.

Community
  • 1
  • 1
David
  • 402
  • 2
  • 9
  • 22
  • Could you post some sample code and show us how you implemented it? – djakapm Nov 25 '12 at 04:34
  • See also [How to get X and Y index of element inside GridLayout?](http://stackoverflow.com/questions/7702697/how-to-get-x-and-y-index-of-element-inside-gridlayout) – trashgod Nov 25 '12 at 10:25
  • Why on earth are you setting the look-and-feel at each iteration of the loop? – Ted Hopp Nov 25 '12 at 23:40

3 Answers3

3

First of all you should register all of your buttons with an actionlistener by this method addActionListener(). Then within the actionPerformed() method you should call getSource() to get a reference to the button which was clicked.

Check this post

Anyway here is the code, The gameButtons[][] array has to be available globally

//A loop to add a new button to each section of the board grid.
for (int i = 0; i < gridSize; i++) 
{
  for (int j = 0; j < gridSize; j++) 
  {
    gameButtons[i][j] = new JButton();
    gameButtons[i][j].addActionListener(this);
    gameButtons[i][j].setBackground(colors[(int)(Math.random() * numColors)]);
    boardGrid.add(gameButtons[i][j]);

    try {
    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) { } 
  }
}

//--------------------------------------------------------


@Override
public void actionPerformed(ActionEvent ae)
{
  for (int i = 0; i < gridSize; i++) 
  {
    for (int j = 0; j < gridSize; j++) 
     {
       if(ae.getSource()==gameButtons[i][j]) //gameButtons[i][j] was clicked
       {
             //Your code here
       }
     }
  }
}
Extreme Coders
  • 3,441
  • 2
  • 39
  • 55
  • I do apologize, I forgot to mention I am doing this inside a static class so using "this" won't work. Is there a way to do this even though I am in a static method without changing the method? – David Nov 25 '12 at 05:35
2

If you want to avoid looping over the arrays again, you can store the indices in the JButton as well.

JButton button = new JButton();
button.putClientProperty( "firstIndex", new Integer( i ) );
button.putClientProperty( "secondIndex", new Integer( j ) );

and then in your ActionListener

JButton button = (JButton) actionEvent.getSource();
Integer firstIndex = button.getClientProperty( "firstIndex" );
Integer secondIndex = button.getClientProperty( "secondIndex" );
Robin
  • 36,233
  • 5
  • 47
  • 99
1

if you need the index of the pressed button, try this:

private Point getPressedButton(ActionEvent evt){
    Object source = evt.getSource();
    for(int i = 0; i < buttons.length; i++){
        for(int j = 0; j < buttons[i].length; j++){
            if(buttons[i][j] == source)
                return new Point(i,j);
        }
    }
    return null;
}

Then you can extract the value by

Point p = getPressedButton(evt);

And this means:

Button pressed == buttons[p.x][p.y]

Otherwise, a simple call evt.getSource(); does the job.

Mordechai
  • 15,437
  • 2
  • 41
  • 82