Link to home
Start Free TrialLog in
Avatar of Suzanne051997
Suzanne051997

asked on

GridBayLayout and setting constraints.

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.
Avatar of roy020697
roy020697

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?
ASKER CERTIFIED SOLUTION
Avatar of Ferris
Ferris

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Suzanne051997

ASKER

Ferris,

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;
        setLayout(gridbag);

        //Add fields to container.

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

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

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

        //Row of components (blank row)
        gridbag.setConstraints(lbl,c);
        add(lbl);
(...)
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 );
loginPanel.add(lUserid);

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

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 );
loginPanel.add(lD1);

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.