File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Fixed Cellsize in GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Fixed Cellsize in GridBagLayout" Watch "Fixed Cellsize in GridBagLayout" New topic

Fixed Cellsize in GridBagLayout

M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64

with the GridBagLayout i am creating a table and in the cells i am showing numbers. The numbers change, depending on what the user does.
The problem is now: When the length of the numbers change, so does the size of the corresponding cell. And when the cell size changes, so does the position of all the other cells and the whole table is moving, which doesn't look very nice. How can i tell the GridBagLayout to keep one cell size, no matter how the cell contents change?

This is my GridBagConstraint:

Thanks in advance!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

Start with setting the weightx to 0.0. With 1.0 it grows when the container is resized.

How To Ask Questions How To Answer Questions
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Um, weightx (and weighty) specify how extra space is to be allotted, so this won't make a difference in a case where the preferredSize of contained components changes.

Bryan, your code snippet lacks one very important detail: what components are used to show the numbers. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

Unless there's more to your question than is apparent from what you've posted, why aren't you using a GridLayout?

luck, db
There are no new questions, but there may be new answers.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

If the changing preferred size is the problem, that can be prevented by creating the text fields with a specified number of columns. Those columns will be used to determine the fixed size.
I agree. Here's the link:
subject: Fixed Cellsize in GridBagLayout
It's not a secret anymore!