Spacejet
Spacejet

Reputation: 131

Center a JPanel with layout GridLayout inside another JPanel

I am attempting to make a Chess game and while trying to work on the GUI, I encountered this issue: I cannot seem to be able to vertically center my chess board on my JFrame. The JPanel is horizontally centered, but it is off-center, stuck to the top, vertically.

Code where the panel using GridLayout is added to its container and the frame is initialized:

public class ChessGUI extends JFrame 
{
    private static final long serialVersionUID = 1L;

    private static Dimension appDimention = new Dimension(1000, 600);

    public static JFrame frame = new JFrame("Chess");
    public static JPanel background = new JPanel();
    public static BoardGUI board = new BoardGUI();

    public static int width;
    public static int height;

    public static void createFrame() 
    {
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Set stuff that JFrame needs
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        //Set stuff that JPanel needs
        background.setPreferredSize(appDimention);

        frame.getContentPane().add(background);
        frame.pack();

        //This 'board' is my Chess Board JPanel which I can't seem to centre
        //'background' is a JPanel which is, as the name suggests, the background
        background.add(board);

        //Set the location of the JFrame and set it visible
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

BoardGUI

@SuppressWarnings("serial")
public class BoardGUI extends JPanel
{
    GridLayout chessBoard = new GridLayout(8, 8);
    Dimension boardDims = new Dimension(500, 500);

    public BoardGUI() 
    {
        this.setLayout(chessBoard);
        this.setBackground(Color.BLACK);
        this.setPreferredSize(boardDims);
    }
}

I'm not really doing anything in the code above to center the BoardGUI object, but I did tried the following two ways with negative results:

background.add(board, JPanel.CENTER_ALLIGNMENT)
background.add(board, BorderLayout.CENTER)

The result I'm getting at the moment:

enter image description here

As you can see it is not vertically centered, and my desired behavior is for it to be both horizontally and vertically centered on the frame.

Any help or insights on any mistakes I might be making would be very welcome! Thanks!

Upvotes: 0

Views: 112

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

background is a JPanel which has a default layout of FlowLayout, which is where your problem is coming from.

I would change

public static JPanel background = new JPanel();

to

public static JPanel background = new JPanel(new GridBagLayout());

Suggestions...

Okay, so suggestions.

  • Avoid static, especially if all you want to do is access information from one class in another - there are better ways to achieve this that won't tightly couple your code
  • Avoid setPreferredSize - it's not a recommend way of defining custom sizing hints, override getPreferredSize instead, this prevents other people from changing it.
  • Instead of setting the preferredSize of the background panel, I would simple make use of either an EmptyBorder or the margins/inserts support of GridBagLayout

Upvotes: 3

Related Questions