Creating a board game layout using JLayeredPane

Solution 1:

In order to initialize the cells of the grid that i want to have images in, don't i need to add them manually in those positions?

If you use a GridLayout every cell must have a component and the components must be added in sequential order. That is as components are added they will wrap automatically to the next row as required.

So even if you don't want an image in a cell you would need to add a dummy component, lets say a JLabel with no text/icon.

An easier approach might be to use a GridBagLayout. The GridBagLayout can be configured to "reserve" space for cells that don't have components. So you can add a component to a specific cell.

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class GridBagLayoutSparse extends JPanel
{
    public GridBagLayoutSparse()
    {
        setBorder( new LineBorder(Color.RED) );

        GridBagLayout gbl = new GridBagLayout();
        setLayout( gbl );
/*
    //  Set up a grid with 5 rows and columns.
        //  The minimimum width of a column is 50 pixels
        //  and the minimum height of a row is 20 pixels.

        int[] columns = new int[5];
        Arrays.fill(columns, 50);
        gbl.columnWidths = columns;

        int[] rows = new int[5];
        Arrays.fill(rows, 20);
        gbl.rowHeights = rows;
*/
        //  Add components to the grid at top/left and bottom/right

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;

        gbc.gridx = 0;
        gbc.gridy = 0;
        addLabel("Cell 0:0", gbc);

        gbc.gridx = 3;
        gbc.gridy = 4;
        addLabel("Cell 3:4", gbc);
    }

    private void addLabel(String text, GridBagConstraints gbc)
    {
        JLabel label = new JLabel(text);
        label.setBorder( new LineBorder(Color.BLUE) );

        add(label, gbc);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("GridBagLayoutSparse");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new GridBagLayout() );
        frame.add(new GridBagLayoutSparse());
        frame.setSize(300, 300);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}
  1. Run the code as is and the components will be grouped together in the center.
  2. Uncomment the block comment and run again. The components will be positioned in the appropriate cell.