Tocchetto Tocchetto -4 years ago 125
Java Question

Avoid blank space between cells when resize

I couldn't find anything that would apply to my problem, neither the solution I tested worked.

So, I'm developing a UI for a course and when I resize the window there's a huge blank space that's created in between the cells, instead of the end as one would expect ( and as I want)

I color coded the panels:

  • Blue- The main panel, Border layout

  • Green - The panel which display the information, Box Layout Y axis

  • Purple - A panel with the title, Box layout X axis (not tied to that,
    can change if needed)

  • Orange- The panel for the controls, Box layout X Axis ( not tied to
    that, can change if needed as long as buttons stay in line like a
    flow layout)

  • Burgundy - The panel with the fields to display/change the
    information, the one with the problem, GridBag layout

So, on the burgundy panel Im using the grid bag because I want the fields and labels to be aligned as they are and this was the simplest way I found to do that. Now, as you can see on the red circle a huge space is created when the window is resized, why? Who is doing that? What panel/ config is the culprit?

Here is some of the code of the parts I believe the be involved, if you need something else let me know

//----Display Panel (green box)
public void createDisplayPanel(){
JPanel dPanel = new JPanel();
add(dPanel, BorderLayout.EAST);
dPanel.setLayout(new BoxLayout(dPanel, BoxLayout.Y_AXIS));

dPanel.add(createDisplayTitlePanel()); //Purple box
dPanel.add(createDisplayFieldsPanel());//Burgundy box
dPanel.add(createDisplayControlsPanel());//Orange box
private JPanel createDisplayFieldsPanel(){
JPanel dFieldsPanel = new JPanel();
GridBagLayout gbl_dFieldsPanel = new GridBagLayout();
gbl_dFieldsPanel.columnWidths = new int[]{0};
gbl_dFieldsPanel.rowHeights = new int[] {0};
gbl_dFieldsPanel.columnWeights = new double[]{Double.MIN_VALUE};
gbl_dFieldsPanel.rowWeights = new double[]{Double.MIN_VALUE};

createDisplayFields(dFieldsPanel); //What really create the fields

return dFieldsPanel;

protected void createDisplayFields(JPanel dFieldsPanel) {

Object[][] inputFieldObjs = {
{ dID, "Residential Property ID:" },
{ dFName, "Property Legal Description:" },
{ dLName, "Property Address:" },
{ dAddress, "City Quadrant:"},
{ dPCode, "Zoning of Property:"},
{ dPhone, "Property Asking Price:" },
{ dType, "Building Square Footage:",clientType },

int row = 0;
for (Object[] obj : inputFieldObjs){

GridBagConstraints gbc_Label = createLabelGBConstrain(row,0);
GridBagConstraints gbc_InputField = createInputFieldGBConstrain(row,1);


if(obj.length == 2)
row ++;

private JLabel createLabel (Object[] objs){
String labelText = (String) objs[1];
JLabel label = new JLabel(labelText);
return label;

private JTextField createTextField(Object[] objs){
JTextField field = (JTextField) objs[0];
field = new JTextField();
return field;

private JComboBox createComboBox(Object[] objs){

JComboBox field = (JComboBox) objs[0];
String[] options = (String[]) objs[2];
field = new JComboBox(options);
return field;

private GridBagConstraints createLabelGBConstrain(int row, int col){

GridBagConstraints gbc_Label = new GridBagConstraints();
gbc_Label.anchor = GridBagConstraints.EAST;
gbc_Label.insets = new Insets(0, 0, 5, 5);
gbc_Label.gridx = col;
gbc_Label.gridy = row;
return gbc_Label;

private GridBagConstraints createInputFieldGBConstrain(int row, int col){

GridBagConstraints gbc_InputField = new GridBagConstraints();
gbc_InputField.anchor = GridBagConstraints.WEST;
gbc_InputField.insets = new Insets(0, 0, 5, 0);
gbc_InputField.gridx = 1;
gbc_InputField.gridy = row;
return gbc_InputField;

Solution I tested:
Set the the weightx and weighty to 0 and the fill to none on the fields/labels... didn't work and I was only able to find this solution that was remotely related to my case

Thank you for any help =)

Edit: Thank you for the answer guys, I the problem really was solved by camickr's solution. I really had no idea what that piece of code was doing. it was auto generated. Also thanks for the tips on comment on how to post a better question, I will sure keep that in mind next time =)

Answer Source

You have to set the weightx and weighty constraints for whatever field you want to contain extra space. You also need to make sure that you anchor is set for that field.

Be sure that the rest of the fields have weightx and weighty set to zero. I usually set them at the beginning to zero and only convert to 1 when I add the last field.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download