Adding buttons using gridlayout

15,003

Solution 1

You need to add the buttons to your JPanel:

public void addButtons(JPanel panel) {
   for (int k = 0; k < SIZE; k++) {
      for (int j = 0; j < SIZE; j++) {
         buttons[k][j] = new JButton(k + 1 + ", " + (j + 1));
         panel.add(buttons[k][j]);
      }
   }
}

Solution 2

// add buttons to the panel INSTEAD of the layout
// experimentLayout.addLayoutComponent("testName", buttons[k][j]);
panel.add(buttons[k][j]);

Further advice:

  1. Don't extend JFrame, just keep a reference to one as long as needed. Only extend frame when adding or changing functionality..
  2. Instead of setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); as seen in this answer.
  3. Instead of setSize(500,500); use panel.setPreferredSize(new Dimension(500,500));. Or better still, extend JButton to make a SquareButton that returns a preferred size equal to the largest preferred of width or height. The last will ensure the GUI is the size it needs to be, square & allowing enough space to show the text.
  4. Instead of setLocationRelativeTo(null); use setLocationByPlatform(true); as seen in the answer linked in point 2.
  5. Add pack() before setVisible(true); which ensures the GUI is the size it needs to be, to display the content.
  6. Instead of setResizable(false) call setMinimumSize(getSize()).
  7. Start & update the GUI on the EDT. See Concurrency in Swing for more details.
Share:
15,003

Related videos on Youtube

Imri Persiado
Author by

Imri Persiado

Updated on September 15, 2022

Comments

  • Imri Persiado
    Imri Persiado over 1 year

    I'm trying to create a simple tic tac toe board made by 9x9 JButtons. I used a 2d array and a gridlayout but the result is nothing, a frame without any button. What I'm doing wrong?

    import java.awt.GridLayout;
    import javax.swing.*;
    
    
    public class Main extends JFrame
    {
        private JPanel panel;
        private JButton[][]buttons;
        private final int SIZE = 9;
        private GridLayout experimentLayout;
        public Main()
        {
            super("Tic Tac Toe");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500,500);
            setResizable(false);
            setLocationRelativeTo(null);
    
            experimentLayout =  new GridLayout(SIZE,SIZE);
    
            panel = new JPanel();
            panel.setLayout(experimentLayout);
    
    
            buttons = new JButton[SIZE][SIZE];
            addButtons();
    
    
            add(panel);
            setVisible(true);
        }
        public void addButtons()
        {
            for(int k=0;k<SIZE;k++)
                for(int j=0;j<SIZE;j++)
                {
                    buttons[k][j] = new JButton(k+1+", "+(j+1));
                    experimentLayout.addLayoutComponent("testName", buttons[k][j]);
                }
    
        }
    
    
        public static void main(String[] args) 
        {
            new Main();
    
        }
    
    }
    

    The addButton method is adding the buttons to the array and straight after to the panel.

  • Imri Persiado
    Imri Persiado over 11 years
    Thank you so much for those advices! I always confused between setsize and preferredsize and the other things you mentioned and I didn't know what to use. thanks again!