Re: Grid Bag question.
From: A. Bolmarcich (aggedor_at_earl-grey.cloud9.net)
Date: Sat, 22 Jan 2005 00:48:28 -0000
On 2005-01-21, Lou Lipnickey <firstname.lastname@example.org> wrote:
> I am trying to put together a panel which needs to consist of 20 lines,
> each containing :
> JLabel - 2 columns
> JTextField - 1 colomn
> JTextField - 1 column
> JLabel - 2 Columns
> The JText fields would be filled out from arrays, so it would be layed
> out with the help of a 'for' loop.
> I put together a test program (provided below) to try to emulate this,
> but the layout manager only seems to want to give each entity (JLabel,
> JTextField) the same amount of width each regardless of what I specify
> in the GridWidth fields.
GridBagLayout does not impose any constraints between the widths of
different columns. Using a gridwidth of 2 simply means that the
component occupies two grid column. The widths of those grid columns
is based on the preferred (or minimum) widths of the components in
In your example, the preferred widths of the JTextFields that span
two columns determine the widths of those two columns. GridBagLayout
is free to distribute the width of the JTextFields between the two
columns any way it wants to. What GridBagLayout does is to set 1) the
width of one column to the width needed by the JTextFields, 2) the
width of the other column to zero.
> Can this be done with specifying sizes for the
> individual JTextfields or labels? Please advise - Thanks - Lou
It can be done using GridBagLayout by setting the perferred and minimum
sizes for the JTextfields and JLabels. You can
1) Set the preferred and minimum widths of the JLabels to be twice that
of the JTextfields.
2) Use a weightx on the JLabels that is twice the weightx of the
3) Use a gridwidth of 1 for all the component.
However, it would be better to use a layout manager that can impose a
constraint between the widths of different columns. TableLayout is one