We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


GridBayLayout and setting constraints.

Medium Priority
Last Modified: 2012-08-13
I am using a GridBagLayout.  My last column is a bunch of textfields.  I use the GridBagConstraints.REMAINDER for each of the textfields that I want to be the last component in the row.  The problem is when I use the line c.ipady = 5 (where c is my instance of GridBagConstraints).  The other columns look okay, but the final column (which contains buttons and textfields) have each component at larger height than the other columns.  Has anyone run into this?

The other thing, I set c.anchor = GridBagConstraints.WEST, yet is seems that the columns with labels keep a CENTER alignment.  How do I set so that the labels are aligned to the left.  I have already set Label.LEFT when the label is instanstiated.
Watch Question

I'm convinced there is something wrong with GridBagLayout, but I've always blamed my lack of knowledge for my problems. I've resorted to adding everything in a row to a panel, then adding the panel to a GridBagLayout, one panel per row in the layout. If this does not help your situation, would you mind posting some code to demonstrate the problem?

Gridbaglayout can be very confusing. It takes a bit of practice.
Have you set the weightx constraint?
The ipady constraint is doing what you request it to do.

ipady is defined:

This field specifies the internal padding, how much to add to the minimum size of the component. The height of the component is at least its minimum height plus ipadx*2 pixels.

That means that you have added extra pixels to the cells height which should make the end cell in the grid higher than the others.  I also think that it is dependent on what is placed in the cell.  If the component that was placed in the cell is larger than the cell itself, I don't think ipady or ipadx has any effect.  Why are you using ipady?

Please respond back and I'll try to help you further...

Good luck...

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts



I have include a portion of my code.  I want to use ipadx because I wish to seperate the components so that there are not tightly packed together.

I guess I'm confused as to what ipadx and ipady do.  In my applet, there is spacing between all of the components now that I specified those constaints, except the components in the last column, which makes the textfields higher, not spaced from the rows about and below it.  Why does ipadx and ipady treat the last column of components different from the other columns?  Also, why are my lables still centered in the column even after I put in the anchor to WEST?

 *  This applet is the StoreLogon page.  For now, this is just the graphics.
import java.awt.*;
import java.util.*;
import java.applet.Applet;

public class StoreLogon extends Applet {
    TextField UserId;  
    TextField FirstName;
    TextField LastName;
    Button Browse;
    Button StoreInfo;
    Button Clear;
    Label lblUserId;  
    Label lblFirstName;
    Label lblLastName;

    public void init() {
        UserId = new TextField(30);  
        FirstName = new TextField(30);
        LastName = new TextField(30);
        Browse = new Button();
        Browse.setLabel("Browse Catalog");
        StoreInfo = new Button();
        StoreInfo.setLabel("Store Information");
        Clear = new Button();
        Clear.setLabel("Clear Info");
        lblUserId = new Label("User ID:  ");  
        lblFirstName = new Label("First Name:  ", Label.LEFT);
        lblLastName = new Label("Last Name:  ", Label.LEFT);

        //Use a GridBagLayout
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.WEST;
        c.ipadx = 5;
        c.ipady = 5;

        //Add fields to container.

        //Row of components
        c.gridwidth = 1;  //reset to default
        c.gridwidth = GridBagConstraints.REMAINDER; //end of row
        c.gridx = 3;
        gridbag.setConstraints(Browse, c);

        //Row of components
        c.gridwidth = 1;  //reset to default
        c.gridwidth = GridBagConstraints.REMAINDER; //end of row
        c.gridx = 3;
        gridbag.setConstraints(StoreInfo, c);

        //Row of components
        c.gridwidth = 1;  //reset to default
        c.gridwidth = GridBagConstraints.REMAINDER; //end of row
        c.gridx = 3;
        gridbag.setConstraints(Clear, c);

        //Row of components (blank row)

Part of the problem with GridBagLayout is that you can specify the wrong code and it will compile.

Another problem is you can specify two (or more) constraints which will essentially cancel out each other or one will stop another from having any effect.  If you want the component to take up all extra horizontal space but then constraint it west, constraining it west will have no effect because you've essentially made the component the same size as the cell which contains it.

The first thing I see is that the following line is wrong:

c.gridwidth = GridBagConstraints.HORIZONTAL;

gridwidth cannot take the HORIZONTAL argument.  The following line is correct.

c.fill = GridBagConstraints.HORIZONTAL;

Further, I wouldn't try to add components one row at a time but rather one cell at a time.  Yes it leads to more code but it helps to visualize what you're actually doing.

This is what I do for a row of cells:

// set up userid label and textfield
Label lUserid = new Label( "User Name:" );
loginc.gridwidth = 1;
loginc.weighty  =  0;
loginc.gridheight = 1;
loginc.weightx = 3;
loginc.fill  =  GridBagConstraints.NONE;
loginGridbag.setConstraints( lUserid, loginc );

loginc.gridwidth = GridBagConstraints.RELATIVE;
loginc.weighty  =  0;
loginc.gridheight = 1;
loginc.weightx = 2;
loginc.fill  =  GridBagConstraints.HORIZONTAL;
loginGridbag.setConstraints(tUserid, loginc);

Label lD1 = new Label( "" );
loginc.gridwidth = GridBagConstraints.REMAINDER;
loginc.weighty  =  0;
loginc.gridheight = 1;
loginc.weightx = 1;
loginc.fill  =  GridBagConstraints.NONE;
loginGridbag.setConstraints( lD1, loginc );

Notice that I added a dummy label that is the last cell in the 'Userid' row.

Give the above a try...

Something to try as a last resort...

Although what I'm about to say may not be clear, it has worked for me in the past and might make more sense in what you're trying to do.  I don't claim that GridBag always works as expected but if you follow the same way of building your GUI components each time, you'll have a better chance of seeing where you're going wrong.

First figure out how many components you want on the screen and then draw a grid around them.  It's evident from the code above that you want two columns and three rows.  Each row consists of two components: a label and a textfield.

Before you continue, add three extra columns (each having three rows).  The idea is that you will have a 'dummy' column to the left of the label column, a 'dummy' column between the label column and the textfield column and a 'dummy' column to the right of the textfield column.  This way you can adjust the weights of the 'dummy' columns to position your six important cells where you want.  

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.