Umzz Mo
Umzz Mo

Reputation: 229

Return the index of clicked button?

I have got an array of 30 buttons []. I have a variable buttonClicked. When I press the button how can I get the index and store the index number in the buttonClicked?

Thanks :)

JButton [] buttons = new JButton[30]; 


        for(int i = 1; i <= 30; i++)
        {       
            int btnNumber = (i > 10 && i <= 20) ? (31 - i) : i;

            System.out.printf("i = %d, btnNumber = %d%n", i, btnNumber);
            buttons[btnNumber - 1] = new JButton("label " + btnNumber);
            //buttons[btnNumber - 1].setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            buttons[btnNumber - 1].setBorder(BorderFactory.createEtchedBorder());
            buttons[btnNumber - 1].setOpaque(true);
            buttons[btnNumber - 1].setBackground(Color.white);

            //Puts the player 1 piece on button 1,3,5,7,9 and player 2 piece on button 2,4,6,8,10 
            if ((btnNumber - 1) < 10) 
            { 
                if (((btnNumber - 1) % 2) == 0) 
                { 
                    buttons[btnNumber - 1].setIcon(piece1); 
                } 
                else 
                { 
                    buttons[btnNumber - 1].setIcon(piece2); 
                } 
            } 
            centerPanel.add(buttons[btnNumber - 1]); 
        } 

//Below is what I am attempting to do, I know is not correct.

public void move()
{
Move = dice.getDiceResult();
int buttonClicked = 0;

if(playerOneTurn =true)
{
buttonclicked + diceResult();
}

//revised

public class MyActionListener implements ActionListener {
Dice dice;
private boolean playerOneTurn = true;
private boolean playerTwoTurn = false;
    @Override
    public void actionPerformed(ActionEvent e) 
{
    String num = e.getActionCommand();
    int index = Integer.parseInt(num);
    int move = dice.getDiceResult();
    int positionLanding = 0;

    if(playerOneTurn = true)
    {
        positionLanding = index + move;
        positionLanding.setIcon("piece1");//how can I set the image Icon to this position?
    }

}
}

Upvotes: 1

Views: 5457

Answers (4)

Andrew Thompson
Andrew Thompson

Reputation: 168845

I prefer the strategy suggested by aioobe, but here is another way.

buttons[btnNumber - 1] = new JButton("label " + btnNumber);
buttons[btnNumber - 1].setActionCommand("" + btnNumber);
// ...

// ...later.. in the actionPerformed() method
String num = actionEvent.getActionCommand();
int index = Integer.parseInt(num);
// ..proceed..

Upvotes: 2

mKorbel
mKorbel

Reputation: 109823

1) putClientProperty

buttons[i][j].putClientProperty("column", i);
buttons[i][j].putClientProperty("row", j);
buttons[i][j].addActionListener(new MyActionListener());

and getClientProperty

public class MyActionListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
    JButton btn = (JButton) e.getSource();
    System.out.println("clicked column " + btn.getClientProperty("column")
            + ", row " + btn.getClientProperty("row"));
}

2) ActionCommand

Upvotes: 5

ControlAltDel
ControlAltDel

Reputation: 35106

The prettiest way is using Component.setName. Then you don't even need to maintain variables with your components - you can go straight off of the name

Upvotes: 2

aioobe
aioobe

Reputation: 421350

You can find the button in ActionEvent.getSource(). To find the index it is just a matter of iterating through the array, looking for that particular button.

Upvotes: 2

Related Questions