Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GridBagLayout

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The grid bag layout is the mother of all layout managers. You can think of a grid bag
layout as a grid layout without the limitations.
...

Weight fields
"You always need to set the weight fields (weightx and weighty) for each area in a grid bag
layout. If you set the weight to 0, then the area never grows or shrinks beyond its initial
size in that direction. In the grid bag layout for Figure 9–29 on page 426, we set the
weightx field of the labels to be 0. This allows the labels to remain a constant width when
you resize the window. On the other hand, if you set the weights for all areas to 0, the
container will huddle in the center of its allotted area rather than stretching to fill it.
Conceptually, the problem with the weight parameters is that weights are properties
of rows and columns, not individual cells. But you need to specify them in terms of
cells because the grid bag layout does not expose the rows and columns. The row
and column weights are computed as the maxima of the cell weights in each row or
column. Thus, if you want a row or column to stay at a fixed size, you need to set the
weights of all components in it to zero.
Note that the weights don’t actually give the relative sizes of the columns. They tell
what proportion of the “slack” space should be allocated to each area if the container
exceeds its preferred size. This isn’t particularly intuitive. We recommend that you set
all weights at 100. Then, run the program and see how the layout looks. Resize the dia-
log to see how the rows and columns adjust. If you find that a particular row or column
should not grow, set the weights of all components in it to zero. You can tinker with
other weight values, but it is usually not worth the effort."

Core Java Vol 1 (8th Ed) P.426/428.

In a nutshell I am a bit confused as to how to see the visual effects of initializing java.awt.GridBagLayout with the weight parameters, and, so far I have tried to update a code snippet, which does not clearly illustrate the above:



Finally I would like to know when it is appropriate to use GridLayout / SpringLayout / FlowLayout? Do you usually use a UI builder? Can I create these layouts and then migrate them to web-interfaces (e.g. JNLP applets or J2EE)?

So far I am aware that these are usually used by tool builders, however, I have noticed that sometimes NetBeans generates lengthy UI generation code which might need to be tweaked in enterprise applications.

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Jon,

I am not very familiar with the GridBagLayout however in your snippet you don't use a GridBagLayout but rather a GridLayout

here



should be something like





 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike ryan wrote:H Jon,

I am not very familiar with the GridBagLayout however in your snippet you don't use a GridBagLayout but rather a GridLayout

here



should be something like






That's right sorry; however it still does not work; in any case the book illustrates a more complicated example, that I would like to simplify for my understanding:

 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fix your first example to use a GridBagLayout.

Its easier to work with 30 lines of code than it is with 200 lines of code.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Fix your first example to use a GridBagLayout.

Its easier to work with 30 lines of code than it is with 200 lines of code.


Yes, that's true, my code was a mess, however, the 200 lines of code is something I need to work up to I guess, in the meantime, here's a minimalist (albeit useless) code snippet that compiles :


 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is about the weightX parameter. Where in the code to you actually use this parameter. How do you expect to see a difference unless you change its value?

Maybe the Swing tuturial examples will be easier for you to understand: http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic