Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

gridbaglayout weights

Thread view: 
Big Jim - 05 Feb 2006 00:30 GMT
If I have this layout with 3 columns:

[comp1 weight=0][comp2 weight=3][comp3 weight=2]
[comp4 weight=1 gridwidth=3                                     ]
[comp5 weight=0.75 gridwidth=2    ][comp6 weight=4]

What would be the effective weight of each column?
Docs say the max in each so would that be:

col1 - 1
col2 - 3
col3 - 4

and what would be the effect of the weights on comp4 and comp5 when resizing
as they span cols with different weights?

Cheers, Richard.
A. Bolmarcich - 06 Feb 2006 16:17 GMT
> If I have this layout with 3 columns:
>
[quoted text clipped - 8 lines]
> col2 - 3
> col3 - 4

With the version of Java that I use, the column weights would be: 0.0,
3.0, 4.0.  What docs "say the max in each"?  You can run a small program
like the following to determine the actual column weights.

import java.awt.*;
                                                                               
 public class Test {
   public static void main(String[] args) {
     Frame f = new Frame();
     GridBagLayout gbl = new GridBagLayout();
     f.setLayout(gbl);
                                                                                 
     GridBagConstraints gbc = new GridBagConstraints();
     gbc.fill = GridBagConstraints.HORIZONTAL;
     gbc.weightx = 0.0;
     f.add(new Label("comp1"), gbc);
     gbc.weightx = 3.0;
     f.add(new Label("comp2"), gbc);
     gbc.weightx = 2.0;
     gbc.gridwidth = GridBagConstraints.REMAINDER;
     gbc.weightx = 1.0;
     f.add(new Label("comp3"), gbc);
     f.add(new Label("comp4", Label.CENTER), gbc);
     gbc.gridwidth = 2;
     gbc.weightx = 0.75;
     f.add(new Label("comp5", Label.CENTER), gbc);
     gbc.gridwidth = GridBagConstraints.REMAINDER;
     gbc.weightx = 4.0;
     f.add(new Label("comp6"), gbc);
     f.pack();
     f.show();
 
     double [] columnWeights = gbl.getLayoutWeights()[0];
     System.out.print("Column Weights =");
     for (int i = 0; i < columnWeights.length; i++) {
       System.out.print(" " + columnWeights[i]);
     }
     System.out.println();
   }
 }

> and what would be the effect of the weights on comp4 and comp5 when resizing
> as they span cols with different weights?

The weights of the components indirectly affect the widths of the
components.  The weights of the components are used to determine the
column weights.  The excess width is distributed among the columns based
on the column weights.  The widths of the components are the widths of
the columns that the components occupy.

In you example (assuming that GridBagConstraints fill is properly set),
the width of comp4 is the width of the 3 columns of the grid and the
width of comp5 is the width of the first two columns.
Big Jim - 06 Feb 2006 17:45 GMT
>> If I have this layout with 3 columns:
>>
[quoted text clipped - 63 lines]
> the width of comp4 is the width of the 3 columns of the grid and the
> width of comp5 is the width of the first two columns.

Hi, cheers for the reply, on the sun site, in the gridbaglayout tutorial,
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html, it
says:

"For each column, the weight is related to the highest weightx specified for
a component within that column, with each multicolumn component's weight
being split somehow between the columns the component is in. Similarly, each
row's weight is related to the highest weighty specified for a component
within that row."

That's why I though the weight for col 1 would be 1.0 as comp 4 has a weight
of 1.0 and it is in col 1 (although it's also in column 2 and 3 as well).
It's the "split somehow" bit I don't like!
Monique Y. Mudama - 06 Feb 2006 17:53 GMT
> Hi, cheers for the reply, on the sun site, in the gridbaglayout
> tutorial,
[quoted text clipped - 10 lines]
> a weight of 1.0 and it is in col 1 (although it's also in column 2
> and 3 as well).  It's the "split somehow" bit I don't like!

I agree.  I've been struggling with GBC.weight lately, too.  A
coworker suggested that I only use weights of 0 and 1 so that I
wouldn't drive myself nuts.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

A. Bolmarcich - 06 Feb 2006 20:45 GMT
>>> If I have this layout with 3 columns:
>>>
[quoted text clipped - 4 lines]
>>> What would be the effective weight of each column?
>>> Docs say the max in each so would that be:
[snip]
>> With the version of Java that I use, the column weights would be: 0.0,
>> 3.0, 4.0.  What docs "say the max in each"?  You can run a small program
>> like the following to determine the actual column weights.
[snip]
> Hi, cheers for the reply, on the sun site, in the gridbaglayout tutorial,
> http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html, it
[quoted text clipped - 9 lines]
> of 1.0 and it is in col 1 (although it's also in column 2 and 3 as well).
> It's the "split somehow" bit I don't like!

By using phrases like "is related" and "split somehow", the description
on the Sun web site is correct while being insufficiently detailed to
be able to determine the actual column weights.

The only sufficiently detailed description of GridBagLayout that I know
is the Java source code.  The code processes the components in order by
their gridwidths.  With your example, after processing the components
with a gridwidth of 1, the column weights are: 0, 3, and 4 (the maximum
of the weight of the component in each column).

Processing the component with a gridwidth of 2 does not change the
column weights because the weight of the component (0.75) is less than
the sum of the previously determined weights of the columns it is in
(0+3).

Processing the component with a gridwidth of 3 does not change the
column weights because the weight of the component (1) is less than
the sum of the previously determined weights of the columns it is in
(0+3+4).

When the weight of a component is greater than the previously
determined weights of the columns it is in, the additional weight is
distributed among the columns in proportion to the previously
determined weights of the columns.  If the previously determined
weights are all zero, the additional weight is added to the last column
occupied by the component.
Big Jim - 06 Feb 2006 22:44 GMT
>>>> If I have this layout with 3 columns:
>>>>
[quoted text clipped - 52 lines]
> weights are all zero, the additional weight is added to the last column
> occupied by the component.

thanks a lot, that's a good description, I'm going to pass this on to Roedy
Green to see if he'd like to include it in his Java info pages.

Cheers, Richard.
Monique Y. Mudama - 06 Feb 2006 22:53 GMT
>> The only sufficiently detailed description of GridBagLayout that I
>> know is the Java source code.  The code processes the components in
>> order by their gridwidths.  With your example, after processing the
>> components with a gridwidth of 1, the column weights are: 0, 3, and
>> 4 (the maximum of the weight of the component in each column).

[snip]

> thanks a lot, that's a good description, I'm going to pass this on
> to Roedy Green to see if he'd like to include it in his Java info
> pages.
>
> Cheers, Richard.

The problem is that anything not published in the documents is likely
to change.  In fact, I suspect that is why the explanation of weights
is so vague -- to allow the possibility that their computation will
change.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Big Jim - 07 Feb 2006 18:50 GMT
>>> The only sufficiently detailed description of GridBagLayout that I
>>> know is the Java source code.  The code processes the components in
[quoted text clipped - 14 lines]
> is so vague -- to allow the possibility that their computation will
> change.

good point!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.