1
public class BoardView extends JFrame
 {

private JButton board [][];

private GameBoard gameboard;

public static JButton cat1 = new JButton();
public static JButton cat2 = new JButton();
public static JButton car1 = new JButton();
public static JButton car2 = new JButton();
public static JButton dog1 = new JButton();
public static JButton dog2 = new JButton();
public static JButton bike1 = new JButton();
public static JButton bike2 = new JButton();

public BoardView()
{
    Container buttonLayout;
    /**
     * Exits the program when closed is clicked
     */
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    /**
     * Sets the title for the Game
     */
    this.setTitle("Memory Of Humanity Game");
    /**
     * Sets the size for the JFrame
     */
    this.setSize(800, 600);
    /**
     * Makes the Pane into a Grid Layout so the Buttons 
     * Line up
     */
    buttonLayout = getContentPane();
    buttonLayout.setLayout(new GridLayout(7, 6));
    /**
     * This adds each JButton to the Pane of the Game
     */
    buttonLayout.add(cat1);
    buttonLayout.add(cat2);
    buttonLayout.add(car1);
    buttonLayout.add(car2);
    buttonLayout.add(dog1);
    buttonLayout.add(dog2);
    buttonLayout.add(bike1);
    buttonLayout.add(bike2)
  }
}

So instead of having to add every JButton one by one like this, how would I create a for loop to do this automatically for me? I have seen a couple on the internet however I don't understand how to loop the the .add part of the JButton. Thank you!

Eddie White
  • 327
  • 2
  • 4
  • 10

2 Answers2

5
for(int i = 0; i < 8; i++) {
    buttonLayout.add(new JButton());
}

This will add 8 JButtons to buttonLayout.

If you need to access them later (which you probably do), you might want to use this:

List<JButton> buttonList = new ArrayList<JButton>();
for(int i = 0; i < 8; i++) {
    JButton button = new JButton();
    buttonList.add(button);
    buttonLayout.add(button);
}

If you want to add a single image to all your buttons:

for(int i = 0; i < 8; i++) {
    ImageIcon image = new ImageIcon("C:/path/to/your/image.jpg");
    JButton button = new JButton(image);
    buttonList.add(button);
}

If you want to add different images to your buttons:

String[] paths = {"C:/1.jpg", "C:/2.jpg", "C:/3.jpg", "C:/4.jpg", "C:/5.jpg", "C:/6.jpg", "C:/7.jpg", "C:/8.jpg"};
for(int i = 0; i < 8; i++) {
    ImageIcon image = new ImageIcon(paths[i]);
    JButton button = new JButton(image);
    buttonList.add(button);
}

Of course, edit the paths according to your needs. Note that the paths can be relative, meaning based on the location of the program.

Cydrick Trudel
  • 9,957
  • 8
  • 41
  • 63
  • Thanks so much that worked perfectly, however I'm also trying to add an image to all of the buttons. This is what I tried but it didn't work. – Eddie White Oct 18 '15 at 18:06
  • @cyderickt ArrayList buttonList = new ArrayList(); for(int i = 0; i < 42; i++) { ImageIcon square = new ImageIcon("square.jpg"); JButton squares = new JButton(square); buttonList.add(squares); buttonLayout.add(squares); } – Eddie White Oct 18 '15 at 18:11
  • I did that the first time and it didn't work, then I tried it again and it worked fine, lol thanks again. One more this man, is there a way to make the picture conform to the size of the JButton instead of it staying at its predetermined size? @cydrickt – Eddie White Oct 18 '15 at 19:04
  • Please refer to http://stackoverflow.com/questions/2856480/resizing-a-imageicon-in-a-jbutton – Cydrick Trudel Oct 18 '15 at 19:06
0

First initialize an array of buttons.

JButton[] buttons = new JButton[8] // instead of having cat1, cat2 ... you have buttons[0], buttons[1] ...

Then do a for loop to initialize and add each button.

for (JButton button : buttons) {
    button = new JButton();
    buttonLayout.add(button);
}
Paul Wasilewski
  • 9,762
  • 5
  • 45
  • 49