GridbagLayout basics

S

Scott Steiner

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);
 
T

Thomas Hawtin

Scott said:
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?
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.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
[...]

Tom Hawtin
 
C

Carl

Scott said:
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);

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.
 
R

Roedy Green

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);

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.

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top