Link to home
Start Free TrialLog in
Avatar of 3nigmatic
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.gif");

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.
Avatar of Mick Barry
Mick Barry
Flag of Australia image



for (int i=0; i<gameDisplay.cards.length; i++) {
     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; i++) {
     buttons[i] = new JButton("Button "+i,cardBack);
     buttons[i].addActionListener(.....
     contentPanel.add(buttons[i]);
}
Avatar of 3nigmatic
3nigmatic

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].addActionListener(); - ';' expected
            ^

Any ideas?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think Provide object in Add Action Listener Methhod


buttons[i].addActionListener(this);
This will work-----

ImageIcon cardBack = new ImageIcon("poker-card-bg.gif");
JButton []buttons = null;

for (int i=0; i<gameDisplay.cards.length; i++) {
         buttons[i] = new JButton("Button "+i,cardBack);
         buttons[i].addActionListener(this);
         contentPanel.add(buttons[i]);
}
      
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].addActionListener(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.
full error message please
buttons[i].addActionListener(this);

with this you have to be sure you you have defined you actionlistener and importanted the right packages.
C:\Program Files\Xinox Software\JCreatorV4\MyProjects\concentrationApp\src\concentrationApp.java:111: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (concentrationApp)
            buttons[i].addActionListener(this);
                         ^

I am importing the right packages and implementing the ActionListener.
sounds like your class does not implement ActionListener