Problem with GridBagLayout's gridheight constraint

Hi, I'm trying to figure out a problem in a large GUI via a small dummy GUI with the same issue. Here's the dummy's code:

publicstaticvoid main(String[] args){

JFrame frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container window = frame.getContentPane();

window.setLayout(new GridBagLayout());

GridBagConstraints c =new GridBagConstraints();

c.anchor = GridBagConstraints.FIRST_LINE_START;

c.fill= GridBagConstraints.NONE;

JPanel one =new JPanel();

JPanel two =new JPanel();

JPanel dud =new JPanel();

one.setBackground(Color.RED);

two.setBackground(Color.BLUE);

c.gridx= 0;

c.gridy= 0;

//c.gridheight = 2;

window.add(one, c);

c.gridx= 1;

c.gridy= 1;

c.gridheight = 1;

window.add(two, c);

c.weightx = 1.0;

window.add(dud, c);

frame.pack();

frame.setVisible(true);

}

When this code is run, I get this:

http://www.freewebs.com/dragon_lagoon/picone.html

When I put the line that has been commented out back in, I expect/want to get this:

http://www.freewebs.com/dragon_lagoon/pictwo.html

What I actually get is this:

http://www.freewebs.com/dragon_lagoon/picthree.html

Does anyone know why this is happening or how to prevent it?

[1787 byte] By [hyperdragon5491a] at [2007-11-27 10:49:52]
# 1

Following works for your simple example. Note that if you change the height of the frame the red panel height also changes:

import java.awt.*;

import javax.swing.*;

public class Test3

{

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container window = frame.getContentPane();

window.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

c.anchor = GridBagConstraints.FIRST_LINE_START;

c.fill= GridBagConstraints.NONE;

c.fill = GridBagConstraints.VERTICAL;

JPanel one = new JPanel();

JPanel two = new JPanel();

JPanel dud = new JPanel();

System.out.println(one.getPreferredSize());

System.out.println(one.getMaximumSize());

one.setBackground(Color.RED);

two.setBackground(Color.BLUE);

c.gridx= 0;

c.gridy= 0;

c.weighty = 1.0;

c.gridheight = 2;

window.add(one, c);

c.gridx= 1;

c.weighty = 0.0;

c.gridheight = 1;

window.add(new JPanel(), c);

c.fill= GridBagConstraints.NONE;

c.gridx= 1;

c.gridy= 1;

c.gridheight = 1;

window.add(two, c);

c.weightx = 1.0;

window.add(dud, c);

frame.pack();

frame.setVisible(true);

}

}

camickra at 2007-7-29 11:21:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

I did a little bit of tweaking to prevent the red one from resizing with the frame (set its weighty to 0 and added another invisible JPanel below it to take up all the space). And then your solution generalized beautifully to the larger GUI! Thanks so much!!!!

hyperdragon5491a at 2007-7-29 11:21:54 > top of Java-index,Desktop,Core GUI APIs...