Solved

GridBayLayout and setting constraints.

Posted on 1997-05-19
5
380 Views
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.
0
Comment
Question by:Suzanne051997
5 Comments
 
LVL 1

Expert Comment

by:roy020697
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?
0
 

Expert Comment

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

Accepted Solution

by:
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...
0
 

Author Comment

by:Suzanne051997
ID: 1220625
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);
(...)
0
 

Expert Comment

by:Ferris
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 );
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.  

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now