Hi,
I want to get the following arrangment of buttons:
# button 0 #
# button 1 ## button 2#
but I'm getting this instead:
# button 0 #
# button 1 #
here's the code, what am I doing wrong?
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JButton button;
button = new JButton("Button 0");
c.weightx = 1;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("Button 2");
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(button, c);
Thomas Hawtin - 18 Jul 2005 20:28 GMT
> Hi,
>
[quoted text clipped - 9 lines]
>
> here's the code, what am I doing wrong?
> button = new JButton("Button 0");
> c.weightx = 1;
[quoted text clipped - 5 lines]
> button = new JButton("Button 1");
> c.weightx = 0.5;
c.gridwidth = 1;
> c.gridx = 0;
> c.gridy = 1;
> pane.add(button, c);
> [...]
Tom Hawtin

Signature
Unemployed English Java programmer
Carl - 18 Jul 2005 20:41 GMT
> Hi,
>
[quoted text clipped - 34 lines]
> c.gridy = 1;
> pane.add(button, c);
One problem I see is that you set the gridwidth to "2", but never set it
back to 1 for the buttons you want to appear side by side. I believe
that will solve your problem.
Carl.
Roedy Green - 18 Jul 2005 22:25 GMT
>button = new JButton("Button 0");
>c.weightx = 1;
[quoted text clipped - 14 lines]
>c.gridy = 1;
>pane.add(button, c);
see
http://mindprod.com/jgloss/gridbaglayout.html
You should not be reusing your constraint object. It leads too easily
to errors like this where you set gridwidth=2 for all your buttons.
You need to set it back to 1.

Signature
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
David Segall - 19 Jul 2005 15:29 GMT
Welcome back Roedy. I have missed your advice.