3nigmatic
asked on
Creating buttons and names with a for loop
Hey,
I am looking to create some buttons from an array, which I can happily do with the following code:
ImageIcon cardBack = new ImageIcon("poker-card-bg.g if");
for (int i=0; i<gameDisplay.cards.length ; i++) {
contentPanel.add(new JButton("",cardBack));
}
But what I cannont work out how to do is give each one of these buttons a unqiue name so that they can be identifiyed later on. Can anyone point me in the right direction please ?
Thanks.
I am looking to create some buttons from an array, which I can happily do with the following code:
ImageIcon cardBack = new ImageIcon("poker-card-bg.g
for (int i=0; i<gameDisplay.cards.length
contentPanel.add(new JButton("",cardBack));
}
But what I cannont work out how to do is give each one of these buttons a unqiue name so that they can be identifiyed later on. Can anyone point me in the right direction please ?
Thanks.
ASKER
I got three compile time errors with that:
buttons[i] = new JButton("Button "+i,cardBack);
replaced with: JButton buttons[i] = new JButton("Button "+i,cardBack);
and the other two I am uncertain about:
buttons[i] = new JButton("Button "+i,cardBack); - ']' expected
^
buttons[i].addActionListen er(); - ';' expected
^
Any ideas?
Thanks.
buttons[i] = new JButton("Button "+i,cardBack);
replaced with: JButton buttons[i] = new JButton("Button "+i,cardBack);
and the other two I am uncertain about:
buttons[i] = new JButton("Button "+i,cardBack); - ']' expected
^
buttons[i].addActionListen
^
Any ideas?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think Provide object in Add Action Listener Methhod
buttons[i].addActionListen er(this);
buttons[i].addActionListen
This will work-----
ImageIcon cardBack = new ImageIcon("poker-card-bg.g if");
JButton []buttons = null;
for (int i=0; i<gameDisplay.cards.length ; i++) {
buttons[i] = new JButton("Button "+i,cardBack);
buttons[i].addActionListen er(this);
contentPanel.add(buttons[i ]);
}
ImageIcon cardBack = new ImageIcon("poker-card-bg.g
JButton []buttons = null;
for (int i=0; i<gameDisplay.cards.length
buttons[i] = new JButton("Button "+i,cardBack);
buttons[i].addActionListen
contentPanel.add(buttons[i
}
ASKER
Hey,
I went with what Objects had supplied in his second comment and added an actionListener:
JButton[] buttons = new JButton[gameDisplay.cards. length];
for (int i=0; i<gameDisplay.cards.length ; i++) {
buttons[i] = new JButton("Button "+1,cardBack);
buttons[i].addActionListen er(this);
contentPanel.add(buttons[i ]);
}
But, I know get an error say I cannot apply an abstractbutton in my constructor.
Without the actionListener it works fine though :D
Thanks.
I went with what Objects had supplied in his second comment and added an actionListener:
JButton[] buttons = new JButton[gameDisplay.cards.
for (int i=0; i<gameDisplay.cards.length
buttons[i] = new JButton("Button "+1,cardBack);
buttons[i].addActionListen
contentPanel.add(buttons[i
}
But, I know get an error say I cannot apply an abstractbutton in my constructor.
Without the actionListener it works fine though :D
Thanks.
full error message please
buttons[i].addActionListen er(this);
with this you have to be sure you you have defined you actionlistener and importanted the right packages.
with this you have to be sure you you have defined you actionlistener and importanted the right packages.
ASKER
C:\Program Files\Xinox Software\JCreatorV4\MyProj ects\conce ntrationAp p\src\conc entrationA pp.java:11 1: addActionListener(java.awt .event.Act ionListene r) in javax.swing.AbstractButton cannot be applied to (concentrationApp)
buttons[i].addActionListen er(this);
^
I am importing the right packages and implementing the ActionListener.
buttons[i].addActionListen
^
I am importing the right packages and implementing the ActionListener.
sounds like your class does not implement ActionListener
for (int i=0; i<gameDisplay.cards.length
contentPanel.add(new JButton("Button "+i,cardBack));
}
you might also want to keep an array of the buttons to identify directly
and also add the appropriate action listener in your loop
for (int i=0; i<gameDisplay.cards.length
buttons[i] = new JButton("Button "+i,cardBack);
buttons[i].addActionListen
contentPanel.add(buttons[i
}