I wrote some code to check this, and it turns out neither of us are right! At least if I'm interpreting the code right.
First of all, the weightx is not a specific percentage, it could be a number above 1 for example, it just links with the other weights to work out a ratio. If only one cell has a weight value, whatever the amount, it takes up the rest of the extra space in the row.
So, given that, if I increase the size of one cell, the weighted cell
will move - but I was still wrong. Because if cell is already at a
minumum size, then it won't change, if there's anywhere to absorb the shock. Here's the code, if anyone's interested:
<pre>
import java.awt.*;
import java.awt.event.*;
public class TwoBags extends Frame {
Frame f;
Panel p1,p2;
GridBagLayout layout;
GridBagConstraints constraints;
Button left,center,right;
public TwoBags() {
super("Two GridBagLayouts");
//first panel
p1 = new Panel();
layout = new GridBagLayout();
p1.setLayout(layout);
constraints = new GridBagConstraints();
constraints.gridx=0;
constraints.gridy=0;
left = new Button("Left");
layout.setConstraints(left,constraints);
p1.add(left);
constraints.gridx=1;
center = new Button("Center");
layout.setConstraints(center,constraints);
p1.add(center);
constraints.gridx=2;
constraints.ipadx=0;
constraints.weightx=.5;
right = new Button("Right");
layout.setConstraints(right,constraints);
p1.add(right);
right=left=center=null;
//second panel
p2 = new Panel();
layout = new GridBagLayout();
p2.setLayout(layout);
constraints = new GridBagConstraints();
constraints.gridx=0;
constraints.gridy=0;
left = new Button("Left");
layout.setConstraints(left,constraints);
p2.add(left);
constraints.gridx=1;
//this time add ipad to center
constraints.ipadx=50;
center = new Button("Center");
layout.setConstraints(center,constraints);
p2.add(center);
constraints.gridx=2;
constraints.ipadx=0;
constraints.weightx=.5;
right = new Button("Right");
layout.setConstraints(right,constraints);
p2.add(right);
f = new Frame();
f.setLayout(new GridLayout(2,1));
f.setSize(300,300);
f.add(p1);
f.add(p2);
f.addWindowListener(
new WindowAdapter(){
public void WindowClosing(WindowEvent event) {
System.exit(0);
}
}
);
f.setVisible(true);
System.out.println(p1.getSize());
System.out.println(p2.getSize());
}
public static void main (
String[] args) {
TwoBags tb = new TwoBags();
}
}
</pre>
Note that if the ipad is set really high, like 250, ALL other buttons are knocked out so they do move. So, the
applet is right, but it is right under only two conditions:
- one col. is at min. size for an object in the col.
- there is extra space for other cols. to absorb (they are not at min size either), after the cell width is changed.
tricky, no?
Eric
[This message has been edited by Eric Barnhill (edited May 12, 2000).]