• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GridBagLayout - can't make sense of what is happening

 
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've spent hours trying to figure out GridBagLayout. I've read the Swing tutorial, looked online, read the posts I could find and still it doesn't make sense.
So I just wrote the below to see how it works and doesn't make sense to me.
I make a row gridy = 0 of buttons size 50x50, insert 30 buttons with gridwidth = 1, gridx 0,1,2 etc.
Next row gridy = 1 of 10 buttons each with gridwidth = 3, gridx 0, 3, 6, etc.
Next row gridy = 2 of 15 buttons I want 2 cells wide and 2 cells tall, gridwidth = 2, gridheight = 2, gridx 0,2,4, etc.
Next row gridy = 4 of 30 buttons gridwidth = 1, gridx 0, 1, 2, etc.

The row of 2 cell wide and 2 cell high is always 2 wide and 1 high. never 2 high.
I tried changing the weighty =1 for top and bottom and weighty=2 for middle - no change. Changing weightx had no effect either. (I guess it shouldn't since weights are used for empty space apportionment)
Changing fill = GridBagConstraints.BOTH expands the buttons to fill the width, but the row is never 2 high.

Why won't the cells in gridy=2 expand to 2x2 as set in the component?

GridBagLayout-fill-both.png
[Thumbnail for GridBagLayout-fill-both.png]
gridbaglayout result with fill = both
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a row of 30 components, each the same size. You got that by adding 1 to the gridy each time and setting the width to 1.
Then you added 3 each time and had width 3 each time and got 10 into the length of the row.

That looks correct from what you are showing. But not the heights. Start by setting the weights (both directions) to the same value throughout. Make sure none of your components has a setSize or setPreferredSize or similar call. Try resizing the display.

Otherwise: don't know. Sorry.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put weights of 0 on the top row, that row will not change size. You have all the weights on the bottom row so the bottom row does all the changing.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still don't know why you aren't getting the double height. What row number are you using for the bottom row? 4 looks correct.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GridBagLayout doesn't provide space for a row or column that has no components in it. Try this code and then uncomment line 40 to see the difference.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An earlier discussion: http://www.java-forums.org/awt-swing/47793-gridbaglayout-vs-me-1-0-a.html

Yeah no new answer for this one
 
Marshall Crenshaw
Greenhorn
Posts: 6
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Believe I found the answer at Pearson informIT website article by Jeff Friesen 9/22/2006 at Harness the Power of Java's GridBagLayout. On page 2 he gives 10 examples of code and the GUI they create. (buildGUI8 is the one where he addresses my problem)

assigning a positive integer to gridwidth (and also to gridheight) to increase the display area can be problematic.
Unless components surround a component whose display area is to be increased, GridBagLayout doesn’t increase the size of the display area.
... corrects the problem.. by surrounding button 1 (on the right and below) with four button components.
GridBaglayout checks for a button immediately to the right of, on each row of, immediately below, and on each column of the display area for button 1. The presence of these buttons results in button 1 obtaining (and sizing to) its two-row by two-column display area


I've never seen this documented anywhere else, but can confirm that behavior still exists in Java 8 in 2015. IMHO one of the reasons for the bad reputation that GridBagLayout seems to have earned.

Code below creates 6 panels in GridBagLayout. Each panel has buttons arranged around a Big button. I use GridBagLayout to size the Big button to 2x2 cells, the remainder of the buttons at 1x1. Text on the buttons is consistent with where should be placed, and (n,n) are (gridx, gridy) used to place each button.

Panel 1: placed a button in first position at (0,0), with Big button below it at (0,1). Another button to right of Big button at expected location (2,1) which would be top right of Big button. Surprisingly, it ignores the 2x2 size for Big button and only makes it 1x1!
Panel 2: place 2 buttons across top at (0,0) and (1,0). Then Big button next row below at (0,1). The Big button expands across the 2 columns as expected, but still refuses to grow in height
Panel 3: place Big button first at (0,0). Then 2 buttons along right hand side at (2,0) and (2,1). Big button now expands vertically to accommodate the 2 buttons to its right, but now won't expand horizontally.
Panel 4: add panel 2 and panel 3 together with 2 across top and 2 to right of Big button, and now it expands to the expected 2x2 cell size. Surrounded it grows.
Panel 5: doesn't matter if you put the other buttons to left or right, and top or bottom, long as both sides are covered. Requiring it to be surrounded is not the behavior I would have expected and quite confusing.
Panel 6: here I placed Big button first, and then placed the other two buttons assuming big button takes 2x2 cells. So Four is on 2nd row down and 3rd column over, Six is 3rd row down and 2nd column over. Refuses to expand Big button at (0,0), so places Four at (1,0) and Six at (0,1).

So it seems you have to fill all the cells across on at least one row, and all the cells down in one column, somewhere in you GridBagLayout for it to function. A lot of other behavior to investigate - can you leave holes in the middle, do the rows have to be immediately adjacent to something that you want to expand, or can they be separated by a few shorter rows, etc. Now I can't trust GridBagLayout to do anything without carefully checking. What do I use to fill in the dummy rows/cols? I found that if you set the buttons to setInvisible(true) the interface acts as if they aren't there (i.e. if you take panel 4 and set Three and Four invisible, you get Panel 2). So if you change visibility during program execution, things could dance around like crazy.
I left out changes to fill and anchor to investigate also. I set everything to fill=BOTH to make it easier to see what size the cells were.



I found an example of how to use gridBagLayout.getLayoutDimensions() to return a 2D array of integers showing the row heights, column widths, horizontal weights, and vertical weights for each cell in the layout. This might help figure out what the layout manager is seeing and doing. I'll post that separately since this is already so long.


GBL-6-examples.png
[Thumbnail for GBL-6-examples.png]
GirdBagLayout 6 examples
 
Marshall Crenshaw
Greenhorn
Posts: 6
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll just post the results of the layout info using gridBagLayout.getLayoutDimensions() on the above 6 examples.

Panel 1 has 4 rows and 3 columns in the layout! Row widths (85, 0, 0, 95) and col heights (26, 0, 26). Looks like it tried to give me 2 rows but ended up with 0 size (maybe they were there before the frame resized itself?)
Panel 2: Rows (85, 86) and Cols (26, 0, 26) - so the missing height is in the column with 0 height?
Panel 3: Rows (0, 52, 95) and Cols (26, 26) - looks like the extra 0 in rows causes a column to fall out?
Panel 4: Rows (85, 86, 95) and Cols (26, 26, 26) - what's expected, no 0's
Panel 5: Rows (84, 80, 0, 95) and Cols (26, 26, 26) - not sure why 4 row widths with one as 0. Why 80 and not 85 or 86? Maybe Six is the shortest label
Panel 6: RRows (0, 80, 87) and Cols (0, 26, 26) - again the 0 widths/heights lead to loss of positions

Well I don't know if I understand it any better, but I see where the problem arises.

Will have to accept "The short explanation is that GBL doesn't compute column widths for columns that don't have at least one component exclusively occupying that column (and similarly for rows)"



I think I have beat this horse enough.
GBL-6-Dimensions.png
[Thumbnail for GBL-6-Dimensions.png]
examples layout dimensions
GBL-6-examples.png
[Thumbnail for GBL-6-examples.png]
examples
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think all the work you have put into exploring that confusing aspect of gird bag deserves a cow.
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic