Hello everyone
As you can see, this is my first post.
We're trying to write a cards game and it uses 12 cards.
Someone taught me to put the buttons in an array, which i think is a really good idea.
I'm encountering one problem, how do i create an action listener for these buttons?
This is my code for now:
(By the way, the cards are divided in 3 rows and 4 columns)
int counter = 0; //number of the card
for(int row =1; row <=3;row++)
{
for(int col=1;col <=4;col++)
{
btnCard[counter] = new JButton();
btnCard[counter].setIcon(new ImageIcon( getClass().getResource( "empty.jpg" )));
addComponent(btnCard[counter],row,col,1,1,0,0);
btnCard[counter].addActionListener
(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
for(int counter = 0; counter < 12; counter++)
{
if(/*This is not right*/btnCard[counter].getActionCommand() == event.getActionCommand())
{
//selectedCards.addCard(gameCards[counter]);
JOptionPane.showMessageDialog(null, "You pushed button " + counter);
}
}
}
}
);
counter ++;
}
}
This code gives te following result: 12 messagedialogs are shown telling "You pushed button 0", "You pushed button 1", ..., "You pushed button 11".
Thanks in advance.
Greets, Kenny.