I am trying to design a grid where every cell in a given column is the
same width, but the leftmost column is twice as wide as the other
columns (which are all the same width). However, when I try using
gridwidth = 2 on the first column and gridwidth = 1 on the others
(except for gridwidth = GridBagConstraints.REMAINDER on the last
column), all of the columns have the same width (pretty much
regardless of whatever weightx and weighty values I assign to the
cells in each column). Even when I hardcode gridx and gridy values
(gridx = 0 and 2 for the first two columns, and 3, 4, 5,... for the
rest), the cells are evenly distributed.
It only seems to work when there is a row with one more column than
the others, and all of them are the same width; in this case, the
first column's cells do span two columns of this one row.
What I am trying to do is something like:
+-----------+-----+-----+
| | | |
| A | B | C |
| | | |
+-----------+-----+-----+
| | | |
| D | E | F |
| | | |
+-----------+-----+-----+
Here's my createGUI():
MainPanel = new JPanel(MainLayout);
Border BlackLine = BorderFactory.createLineBorder(Color.black);
JLabel LabelA = new JLabel("A", SwingConstants.CENTER);
LabelA.setBorder(BlackLine);
JLabel LabelB = new JLabel("B", SwingConstants.CENTER);
LabelB.setBorder(BlackLine);
JLabel LabelC = new JLabel("C", SwingConstants.CENTER);
LabelC.setBorder(BlackLine);
JLabel LabelD = new JLabel("D", SwingConstants.CENTER);
LabelD.setBorder(BlackLine);
JLabel LabelE = new JLabel("E", SwingConstants.CENTER);
LabelE.setBorder(BlackLine);
JLabel LabelF = new JLabel("F", SwingConstants.CENTER);
LabelF.setBorder(BlackLine);
GridBagConstraints C = new GridBagConstraints();
C.fill = GridBagConstraints.BOTH;
C.gridwidth = 2;
C.gridheight = 1;
C.weightx = 1.0;
C.weighty = 2.0;
C.gridwidth = 2;
MainLayout.setConstraints(LabelA, C);
MainPanel.add(LabelA);
C.gridwidth = 1;
MainLayout.setConstraints(LabelB, C);
MainPanel.add(LabelB);
C.gridwidth = GridBagConstraints.REMAINDER;
MainLayout.setConstraints(LabelC, C);
MainPanel.add(LabelC);
C.gridwidth = 2;
MainLayout.setConstraints(LabelD, C);
MainPanel.add(LabelD);
C.gridwidth = 1;
MainLayout.setConstraints(LabelE, C);
MainPanel.add(LabelE);
C.gridwidth = GridBagConstraints.REMAINDER;
MainLayout.setConstraints(LabelF, C);
MainPanel.add(LabelF);
getContentPane().add(MainPanel, BorderLayout.CENTER);
However, what I get is:
+-------+-------+-------+
| | | |
| A | B | C |
| | | |
+-------+-------+-------+
| | | |
| D | E | F |
| | | |
+-------+-------+-------+
Does anybody know if there's something painfully obvious I'm missing
here? Is it possible to make every cell in a column twice as wide as
the cells in the other columns?
-- Don
Larry Barowski - 02 Feb 2005 07:28 GMT
> I am trying to design a grid where every cell in a given column is the
> same width, but the leftmost column is twice as wide as the other
[quoted text clipped - 4 lines]
> regardless of whatever weightx and weighty values I assign to the
> cells in each column).
The sizes of columns are determined by the preferred width
or minimum width of the components in that column, with
any "extra" horizontal space assigned according to weightx.
All else being equal, changing the gridwidth for all elements
in a column will not change anything. If you subclass all of
the components so that getPreferredSize() and
getMinimumSize() return a Dimension with 0 width, then
using weight x = 2 for the first column and 1 for the others
will do what you want, since all the horizontal space will be
"extra" space - but that would be a ridiculous way to do this.
Creating your own layout manager to do what you want in
this case is quite simple.
Don Del Grande - 03 Feb 2005 04:09 GMT
>> I am trying to design a grid where every cell in a given column is the
>> same width, but the leftmost column is twice as wide as the other
[quoted text clipped - 17 lines]
>Creating your own layout manager to do what you want in
>this case is quite simple.
I've heard two different things about creating layout managers; "it's
easy to develop a layout manager to your specifications" and "try
making sure it works under all conditions and on all platforms". I
did write one that appears to do what I want, but I did have one
question: what values do I return for preferredLayoutSize() and
minimumLayoutSize()? Since the grid is meant to take up the entire
component, I just return the parent component's size for both of them,
which appears to work, but something tells me it's not right
(especially if the layout manager is used for something besides an
applet/application's content pane).
(Also, I noticed that if I added a dummy row with an extra cell, set
gridwidth = 1 for all cells except the last one, and weighty = 0.001,
then everything appears to work the way I expected - the dummy row is
so thin that it takes up no visible space.)
-- Don
Larry Barowski - 03 Feb 2005 15:23 GMT
> I've heard two different things about creating layout managers; "it's
> easy to develop a layout manager to your specifications" and "try
[quoted text clipped - 6 lines]
> (especially if the layout manager is used for something besides an
> applet/application's content pane).
Preferred size should be the minimum such that
all elements have at least their preferred size.
Minimum size should be the minimum such that
all elements have at least their minimum size.
Note: the desired layout was a grid with all
columns of equal width except that column zero
is double the width of the others.
I assume you are using actual element heights for
each row. In that case the preferred height should
be the sum of the largest preferred height in each
row, and do the similar thing for minimum height.
Your layout should enforce this - if their is enough
height for all preferred heights to be met, then
layout according to preferred heights, otherwise
layout according to minimum heights.
The preferred width should be
(max_pref_width() * num_cols + 1). Where
max_pref_width is the maximim of the preferred
widths of all elements not in column zero, and
1/2 the preferred widths for elements in column
zero. That will be the minimum size that
guarantees that all elements are at least their
preferred width. And again, do the similar thing
for minimum width.
John McGrath - 02 Feb 2005 08:18 GMT
> I am trying to design a grid where every cell in a given column is the
> same width, but the leftmost column is twice as wide as the other
> columns (which are all the same width). However, when I try using
> gridwidth = 2 on the first column and gridwidth = 1 on the others
> (except for gridwidth = GridBagConstraints.REMAINDER on the last
> column), all of the columns have the same width
That is not how GridBagLayout works. The gridx, gridy, gridwidth, and
gridheight values affect the relative positions of the components, not the
size of the cells. The cell size are primarily based on the preferred
sizes of the components.
> (pretty much regardless of whatever weightx and weighty values I
> assign to the cells in each column).
The weightx and weighty values affect how any extra (or lacking) space is
distributed amongst the rows and columns.

Signature
Regards,
John McGrath