shardulc
shardulc

Reputation: 301

GridBagLayout Weights and Overlapping Components

Suppose I have a JPanel with a GridBagLayout like this:

-----------------------------------------------
|      |               |               |      |   
|      |Left JLabel 1  | Right JLabel 1|      |   
|      |               |               |      |   
|-glue-|Left JLabel 2  | Right JLabel 2|-glue-|
|      |               |               |      |   
|      |         Bottom JLabel         |      |   
|      |               |               |      |   
|                vertical glue                |   
-----------------------------------------------

The left/right labels each span 1 row and 1 column, while the bottom label spans 1 row and 2 columns. The horizontal glues on both sides span 3 rows and 1 column each. The vertical glue at the bottom spans 1 row and 4 columns.

Each of the labels has a weighty of 1.0. Further, the glues on both sides also have a weighty of 1.0. The vertical glue has a weighty of 1.5. Currently, when I resize the panel in the y-direction, the vertical glue gets approximately 3/5ths of the available space and the entire upper portion gets approximately 2/5ths of the available space.

My question is, how does the weighting work when the glue overlaps (when viewed from the y-resize perspective) with the buttons? Without the glue, the total weight of the buttons would have been 3.0: this is a completely different resizing scenario! How is the weight actually calculated (highest, average, 'seen from a resize perspective', ?) and used?

Upvotes: 0

Views: 307

Answers (1)

camickr
camickr

Reputation: 324128

Well, the calculation is rather complex so who knows exactly what is happening.

I would guess you are getting something like:

  1. The "vertical glue" is considered one row of data so it gets 1.5.
  2. The "glue" components are consider another row of data so they get 1.0.
  3. Then within the "glue" components you have 3 more rows of data and they each get .33 of the space allocated to the "glue" components.

Upvotes: 2

Related Questions