Java – Gap between JPanels in GridLayout

awtgrid layoutjavalayout-managerswing

The code below produces nine individual JPanels in which are 9 buttons. The nine JPanels are arranged onto a base JPanel using GridLayout. This base JPanel is then placed onto the ContentPane using Border Layout. I'm using borders for the JButtons and each individual JPanel to clearly define their separation. The JButtons inside each JPanel look fine but there is a gap between the JPanels which is causing the appearance of a double line which is bugging the hell out of me. I've tried adding each individual JPanel onto the ContentPane directly but this does nothing. Totally stumped and wondering if anyone has any suggestions?

P.S. I know the mass of duplicate code needs re-factoring in a bad way and it will, this is just for mock-up purposes.

Thanks,

import java.awt.*;
import javax.swing.*;
import javax.swing.UIManager.*;

public class View1 extends JFrame
{
    private JPanel      basePanel;
    private JPanel      childPanel1;
    private JPanel      childPanel2;
    private JPanel      childPanel3;
    private JPanel      childPanel4;
    private JPanel      childPanel5;
    private JPanel      childPanel6;
    private JPanel      childPanel7;
    private JPanel      childPanel8;
    private JPanel      childPanel9;
    private int         row = 3;
    private int         column = 3;
    private JButton[][] squares1;
    private JButton[][] squares2;
    private JButton[][] squares3;
    private JButton[][] squares4;
    private JButton[][] squares5;
    private JButton[][] squares6;
    private JButton[][] squares7;
    private JButton[][] squares8;
    private JButton[][] squares9;
    private Font        numberFont;

public View1()
{
    setSize(400,400);
    setTitle("2013");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    basePanel  = new JPanel();
    basePanel.setLayout(new GridLayout(row, column, 0, 0));
    numberFont = new Font("sansserif", Font.BOLD, 32);

    childPanel1Setup();
    childPanel2Setup();
    childPanel3Setup();
    childPanel4Setup();
    childPanel5Setup();
    childPanel6Setup();
    childPanel7Setup();
    childPanel8Setup();
    childPanel9Setup();

    basePanel.add(childPanel1);
    basePanel.add(childPanel2);
    basePanel.add(childPanel3);
    basePanel.add(childPanel4);
    basePanel.add(childPanel5);
    basePanel.add(childPanel6);
    basePanel.add(childPanel7);
    basePanel.add(childPanel8);
    basePanel.add(childPanel9);

    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    cp.add(basePanel, BorderLayout.CENTER);
}

public void childPanel1Setup()
{
    childPanel1 = new JPanel();
    childPanel1.setLayout(new GridLayout(row, column, 0, 0));
    childPanel1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares1 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares1[rows][cols] = new JButton();
            squares1[rows][cols].setSize(32, 32);
            squares1[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares1[rows][cols].setBackground(Color.WHITE);
            childPanel1.add(squares1[rows][cols]);
        }
    }
}

public void childPanel2Setup()
{
    childPanel2 = new JPanel();
    childPanel2.setLayout(new GridLayout(row, column, 0, 0));
    childPanel2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares2 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares2[rows][cols] = new JButton();
            squares2[rows][cols].setSize(32, 32);
            squares2[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares2[rows][cols].setBackground(Color.WHITE);
            childPanel2.add(squares2[rows][cols]);
        }
    }
}

public void childPanel3Setup()
{
    childPanel3 = new JPanel();
    childPanel3.setLayout(new GridLayout(row, column, 0, 0));
    childPanel3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares3 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares3[rows][cols] = new JButton();
            squares3[rows][cols].setSize(32, 32);
            squares3[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares3[rows][cols].setBackground(Color.WHITE);
            childPanel3.add(squares3[rows][cols]);
        }
    }
}

public void childPanel4Setup()
{
    childPanel4 = new JPanel();
    childPanel4.setLayout(new GridLayout(row, column, 0, 0));
    childPanel4.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares4 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares4[rows][cols] = new JButton();
            squares4[rows][cols].setSize(32, 32);
            squares4[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares4[rows][cols].setBackground(Color.WHITE);
            childPanel4.add(squares4[rows][cols]);
        }
    }
}

public void childPanel5Setup()
{
    childPanel5 = new JPanel();
    childPanel5.setLayout(new GridLayout(row, column, 0, 0));
    childPanel5.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares5 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares5[rows][cols] = new JButton();
            squares5[rows][cols].setSize(32, 32);
            squares5[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares5[rows][cols].setBackground(Color.WHITE);
            childPanel5.add(squares5[rows][cols]);
        }
    }
}

public void childPanel6Setup()
{
    childPanel6 = new JPanel();
    childPanel6.setLayout(new GridLayout(row, column, 0, 0));
    childPanel6.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares6 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares6[rows][cols] = new JButton();
            squares6[rows][cols].setSize(32, 32);
            squares6[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares6[rows][cols].setBackground(Color.WHITE);
            childPanel6.add(squares6[rows][cols]);
        }
    }
}

public void childPanel7Setup()
{
    childPanel7 = new JPanel();
    childPanel7.setLayout(new GridLayout(row, column, 0, 0));
    childPanel7.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares7 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares7[rows][cols] = new JButton();
            squares7[rows][cols].setSize(32, 32);
            squares7[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares7[rows][cols].setBackground(Color.WHITE);
            childPanel7.add(squares7[rows][cols]);
        }
    }
}

public void childPanel8Setup()
{
    childPanel8 = new JPanel();
    childPanel8.setLayout(new GridLayout(row, column, 0, 0));
    childPanel8.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares8 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares8[rows][cols] = new JButton();
            squares8[rows][cols].setSize(32, 32);
            squares8[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares8[rows][cols].setBackground(Color.WHITE);
            childPanel8.add(squares8[rows][cols]);
        }
    }
}

public void childPanel9Setup()
{
    childPanel9 = new JPanel();
    childPanel9.setLayout(new GridLayout(row, column, 0, 0));
    childPanel9.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares9 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares9[rows][cols] = new JButton();
            squares9[rows][cols].setSize(32, 32);
            squares9[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares9[rows][cols].setBackground(Color.WHITE);
            childPanel9.add(squares9[rows][cols]);
        }
    }
}

}

Best Answer

I believe it's because the Layout Manager for JPanels is FlowLayout which includes a margin. When you create the panels, set their layout to GridLayout which will give you 0 margin.

Also have you done setVgap(0); and setHgap(0); for the GridLayout?