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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With