GridBayLayout and setting constraints.

Posted on 1997-05-19
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.
Question by:Suzanne051997
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1220622
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?

Expert Comment

ID: 1220623
Gridbaglayout can be very confusing. It takes a bit of practice.
Have you set the weightx constraint?

Accepted Solution

Ferris earned 50 total points
ID: 1220624
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...

Author Comment

ID: 1220625

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)

Expert Comment

ID: 1220626
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.  


Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question