1

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)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
geri
  • 57
  • 1
  • 2
  • 6
  • *"it's not guaranteed that there's gonna be a button in every row or column"* Columns and rows can span multiple cells. – Andrew Thompson May 14 '17 at 16:29
  • 1
    But now I look at the image, that would be easiest to achieve using a `GridLayout` The 9*9 buttons (or labels) might contain a black icon, a white icon, or a transparent icon. See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556) for one implementation. – Andrew Thompson May 14 '17 at 16:36

1 Answers1

2

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

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • In this case I should fill up the whole layout with Labels, cuz I dont know where the skips should be, then just change some of it with the buttons later. Am I right? or is there a better solition? – geri May 14 '17 at 15:33
  • anyway, this fixes my problem, I'm just curios, thanks for your help :) – geri May 14 '17 at 15:40
  • Yes you can. Besides thats what layout groups are for. Did you check documentation for `GridBagLayout`? It has `Glue`s, fillers and stuff – Antoniossss May 14 '17 at 15:44
  • I think I'm gonna stick with the JLabels, those works just fine actualy :) anyway, thanks for your help, much appreciated :) – geri May 14 '17 at 16:28