GridLayout apila todos los JPanels en la primera celda

I am trying to create a map for a game I am making with a JPanel that uses gridLayout. In my first tests I use a 5x5 grid and create my small panels which are a subclass of JPanel. My program creates them fine but when I add all of the panels into the larger panel and display it, only the first square shows up and the rest is blank. Why does it do that? Here is my code for the MapSpace(smaller panel):

import javax.swing.*;
import java.awt.*;
public class MapSpace extends JPanel{
    private int ownerTag;
    private int xPos, yPos;
    public MapSpace(){
        xPos = 0;
        yPos = 0;
        ownerTag = 0;
public MapSpace(MapSpace m){
        xPos = m.getX();
        yPos = m.getY();
        ownerTag = m.getID();

and here is my code for the Map:

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Map extends JPanel{
    private int cols, rows;
    private int randCol, randRow;
    private MapSpace[][] spaces;
    Random gen = new Random();
    public Map(int w, int h){
        cols = h;
        rows = w;
        setLayout(new GridLayout(cols, rows));
        spaces = new MapSpace[cols][rows];
        for(int i = 0; i < cols; i++){
            for(int j = 0; j < rows; j++){
                MapSpace panel = new MapSpace(i, j);
                spaces[i][j] = panel;
        for(int i = 0; i < cols; i++){
            for(int j = 0; j < rows; j++){
                MapSpace spot = new MapSpace(spaces[i][j]);
        setSize(400, 400);

the second nested for loop is where all the mapSpaces are added but when I put the map in a JFrame and display it in a GUI window only one small square in the top left corner appears.

preguntado el 24 de mayo de 14 a las 16:05

Reemplaza MapSpace JLabel in Map class and look what it displays? -

Otherwise, create an , then this issue will probably be solved within a few minutes. -

@Braj I replaced MapSpace with JLbael and it puts the labels in a nice 5x5 grid on the window. What does this mean? -

1 Respuestas

Why are you trying to create a MapSpace with an instance of MapSpace?

Just create the MapSpace with the parameters you want and then add the MapSpace to your Array and the panel at the same time.

and display it in a GUI window only one small square in the top left corner appears.

Probably because you don't give a preferredSize() size to your MapSpace class so it defaults to (10, 10) which is the size for a panel using a FlowLayout with no components added to it. So since you create a 5x5 grid you probably see a (50, 50) white square.

Anular el getPreferredSize() of your MapSpace class to return the default Dimension for each square.

contestado el 24 de mayo de 14 a las 17:05

Okay I added it to the panel and array at the same time and I tried changing the preferred size but there's still just a square in the top left. Some panels are blue and some are red so it shows a red square and a blue line on the right like it was placed over the blue panel. - enlodar

Well I can't tell where blue/red squares come from since the code you posted only uses WHITE. All I can suggest is that you read the Swing tutorial on How to Use GridLayout for a working example. Compare your code with the tutorial to see what is different. - camickr

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.