Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gridlayout only adds elements vertically, not horizontally

I'm making a level editor for a simple game I'm creating, in the level editor I want to have a few buttons and labels that are aligned on a grid. Pretty much what gridlayout was designed to do.

The following code, for some unholy reason that is beyond me, only adds elements underneath each other, like this:

Map Width:

Map Height:

Map Depth:

it -should- look like this:

Map Width: Map Height:

Map Depth:

I've been trying for an hour and I'm pretty much stumped, this should not take as much effort as it does to get it working, yet it does.

    private void drawUiElements()
{
    int xLoc = (int) (dim.width * 0.75);
    int yLoc = 0;
    int width = (int) (dim.width * 0.25);
    int height = dim.height;

    JPanel buttonContainer = new JPanel();
    buttonContainer.setLayout(new GridLayout(16, 2, 5, 5));
    buttonContainer.setBounds(xLoc, yLoc, width, height);
    buttonContainer.setName("buttonContainer");

    JLabel labelx = new JLabel("Map Width:");
    JLabel labely = new JLabel("Map Height:");
    JLabel labelz = new JLabel("Map Depth:");

    buttonContainer.add(labelx, "1");
    buttonContainer.add(labely, "2");
    buttonContainer.add(labelz, "3");

    add(buttonContainer);
}

Thanks.

like image 277
user1870238 Avatar asked Dec 13 '25 10:12

user1870238


1 Answers

You can't add components to a specific cell when using GridLayout. You could set the initial number of rows to 0 so that the components are filled rows-first.

buttonContainer.setLayout(new GridLayout(0, 2, 5, 5));

See: How to Use GridLayout

like image 109
Reimeus Avatar answered Dec 16 '25 11:12

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!