File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes GridBag gremlins! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GridBag gremlins!" Watch "GridBag gremlins!" New topic
Author

GridBag gremlins!

Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
I'm using the GridBagLayout on a JPanel to show columns of JTextFields and their corresponding JLabels (as the column headers).

Everything shows us nice and aligned in all of the columns, but the columns are not equally spaced!

The first three are in the position they should be, with the first component being a JLabel (holding a name for each row), but after that, the spacing gets larger (a little at first, then a lot, then less, but still a lot) between the other columns which are JTextFields.

I checked the gridx, the weight, and the width and all of them are as they should be (gridx goes 0, 2, 4, 8 ,10..width is 1, weight is 0.0).

Any idea of what it is I'm doing wrong?



Here is a snippet of the code:

private void init()
{
this.setLayout(new GridBagLayout());
this.setAlignmentX(JPanel.CENTER_ALIGNMENT);

mainLabel.setFont(new Font("SanSerif", Font.BOLD, 12));
mainLabel.setForeground(labelsColor);

grandTotalTextField.setBackground(totalsColor);

createVariables();

//Now draw
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 2, 2 , 2);
gbc.gridx = 6;
gbc.gridy = 0;
gbc.ipadx = 0;
gbc.ipady = 0;

this.add(mainLabel, gbc);

gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy += 2;
gbc.gridwidth = 1;

this.add(photoLabel, gbc);

for(int x = 0; x < TOTAL_COLS; x++)
{
gbc.gridx += 2;

this.add(columnLabels[x], gbc);
}

for(int x = 0; x < TOTAL_ROWS; x++)
{
gbc.gridx = 0;
gbc.gridy++;

this.add(imageLabels[x], gbc);

for(int y = 0; y < TOTAL_COLS; y++)
{
gbc.gridx += 2;

this.add(valuesTextField[x][y], gbc);

}
}

gbc.gridy++;
gbc.gridx = 8;

this.add(totalLabel, gbc);

gbc.gridx += 2;

grandTotalTextField.setPreferredSize(preferredDim);
grandTotalTextField.setEnabled(false);

this.add(grandTotalTextField, gbc);

}
Norm Miller
Ranch Hand

Joined: May 21, 2002
Posts: 56
Hi,

I may be dead wrong, but when I am planning a layout where I have a requirement to line everything up "just so", I don't think of gridbag layout.

I have had good luck with grid layout in cases like this because it likes to set the grid sizes to the largest component you are using to build it. If you have java 1.4, you can also consider the "Springs" layout. There is an article on it [URL=http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.htmlhere.[/URL]
[ June 22, 2004: Message edited by: Norm Miller ]
Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
I'll look into Springs.

Still, I'd think that GridBag should work great here...each component to its own X/Y position.
Norm Miller
Ranch Hand

Joined: May 21, 2002
Posts: 56
I understand what you are saying. Gridbag will certainly keep the X-Y relations right, but it is tough to get it nailed down that last little bit -- as you have found out.
Eric Snell
Greenhorn

Joined: Jun 09, 2004
Posts: 28

It's hard to envision exactly what the problem is. My guess is that what your are seeing is related to the preferred size of the components. Try setting them all equal and see if that helps.

Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
I tried setting the preferred sizes the same, but that didn't help.

This is really odd behavior.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I tried to divine what you are attempting from your code snippet. Here's a solution and some observations/comments.

The explicit setting of gridx and gridy values is the most difficult way to use GridbagLayout in my experience. These kind of layouts are very compact and spiffy when you get them all worked out but it is tough to do so.

Here is another approach to GridBag that is easy to set up and maintain once you catch on to the technique. Leave the gridx and gridy constraints alone as much as possible and use gbc.gridwidth = gbc.REMAINDER to jump to the next row. This makes for more fluid layouts in general and less time lost in chasing that odd out–of–place value or size glitch.

Other tricks are to use pack (vs. setSize) while trouble–shooting and to do one line (little bits) at a time so you can see what's going on. Too much at once gets overwhelming and leads to despair.

About the use of gridy in the code below: with the first row set with gbc.gridwidth = COLS I was obviously unable to use the gbc.REMAINDER value for changing rows. So I used the explicit gbc.gridy = 1 to jump to the next row. Then in the following row [2] I reset the gridy to its default value of gbc.RELATIVE.
Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
Wow! Thanks for the effort.

I switched to SpringLayout as the first responder mentioned and got everything to work fine using it, but I will try your solution out as well in case I need the GridBag for some future reason.


OK...I just tested it out and it works great.

I'm still curious as to what I was doing wrong though as I checked that each and every gridx position, each and every weight, and each and every width was as it should be.
[ June 22, 2004: Message edited by: Darrin Smith ]
Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
Let me take the "great" part back.

It looks better, but the distance between the columns still isn't right (some more spaced than others)

I think the GridBagLayout is doing something that I don't understand. The SpringLayout seems to do what I want and do it better (everything is evenly spaced and aligned).
[ June 22, 2004: Message edited by: Darrin Smith ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GridBag gremlins!