I am running into a problem with GridBagLayouts.
I have a few components laid out as shown in the picture below:
I want the "center" component to be able to move above the bottom edge of "top-right" if the "top" component shrinks small enough. However, this obviously causes a problem since it would have to be in a different row in the grid to be able to do so.
To solve this - I define the entire right column as its own container, with its own layout manager. I'd expect the two columns to be able to behave independently, however I get the same problem! I don't see how the two layout managers could possibly be interacting still. Can anyone possibly explain what my problem is?
Here is the relevant code (in my class extending Applet):
public void init(){
addComponents();
}
private void addComponents(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
Left left = new Left();
Top top = new Top();
Center center = new Center();
TopRight topRight = new TopRight();
BottomRight bottomRight = new BottomRight();
Bottom bottom = new Bottom();
topRight.setPreferredSize(new Dimension(200,200));
top.setPreferredSize(new Dimension(0,200));
center.setPreferredSize(new Dimension(0,100));
container.setPreferredSize(new Dimension(200,200));
c.fill = GridBagConstraints.BOTH;
c.gridx = 0; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 3;
c.weightx = 0.2; c.weighty = 1.0;
this.add(left,c);
c.gridx = 1; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 0.8; c.weighty = 0.8;
this.add(top,c);
c.gridx = 1; c.gridy = 1;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 0.8; c.weighty = 0.0;
this.add(center,c);
c.gridx = 1; c.gridy = 2;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 0.8; c.weighty = 0.2;
this.add(bottom,c);
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
c.gridx = 2; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 3;
c.weightx = 0.0; c.weighty = 1.0;
this.add(container,c);
GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.BOTH;
c2.gridx = 0; c2.gridy = 0;
c2.gridwidth = 1; c2.gridheight = 1;
c2.weightx = 0.0; c2.weighty = 0.0;
container.add(topRight,c2);
c2.gridx = 0; c2.gridy = 1;
c2.gridwidth = 1; c2.gridheight = 1;
c2.weightx = 0.0; c2.weighty = 1.0;
container.add(bottomRight,c2);
}
Note: Each of the components shown (left, top, etc) is an extension of JPanel - each with its paint(Graphics g) method overridden to fill it with some color.
Thanks in advance, and sorry for the long read,
Jonathan
I fixed the problem. It had something to do with me setting the preferred height of the Top component unnecessarily - when I removed that line it worked like I was wanting.