Search code examples
javaswinggridbaglayout

Is there a way to set fixed rows and columns in GridBagLayout?


I'm kinda new to GUI programming, and as I know, and as it shows, the GridBagLayout will merge empty rows or columns. My problem is that I have to add buttons on random positions, and it's not guaranteed that there's gonna be a button in every row or column, but I should keep the empty lines anyway.

I'm using GridBag cuz i have to change positions on some buttons and it seems handy for that.

So the question is: Is it possible to fix the rows and columns in a GridBagLayout? Or keep the empty lines somehow.

there is an example of the state of the buttons:

Random Buttons

(there should be 9x9 positions, but as shown some of the lines are merged)


Solution

  • If you need to "skip" some cells, just add a filler, for example empty new JLabel() where there will be "no button".