This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes GridBagLayout question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GridBagLayout question" Watch "GridBagLayout question" New topic

GridBagLayout question

Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
I've always avoided layoutmanager precisely because they never seem to behave the way I want, like in this case. It's simple enough, well I though so anyway.

I have 3 panels which are laid out as I wanted but I want to put 4 labels at the top left corner of panel 3 but c.anchor = GridBagConstraints.FIRST_LINE_START; does nothing, the labels stay in the middle of the container.
The description of the anchor attribute says: Used when the component is smaller than its display area to determine where (within the area) to place the component.. Well the display area (window3) is 512 X 768 pixels which is much larger than the label components, I've even tried setting a fixed size to the labels but the outcome is the same. What am I doing wrong here?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43957
This thread would sit better on the Swing forum. Moving.
pete stein

Joined: Feb 23, 2007
Posts: 1561
We'll be better able to help you if you can post a small compilable and runnable program that demonstrates the layout and does nothing else. This way we can see for ourselves your problem and also this allows us to modify your code and probably help you find a solution sooner.

One thing I want to make sure that you fully understand is that it is very common to use multiple layouts in one GUI, for instance have the main JPanel use one layout, say BorderLayout for instance and have sub-JPanels each use their own layouts.

Also many of us avoid using GridBagLayout due to its complexity, but if you are going to use it, you'll need to read up on it as much as you can to understand all its intricacies. For instance, I don't see where you set height, width, fill, insets, and especially weights, since without weights, components tend to bunch up in the middle.

Much luck!
David A. Smith

Joined: Sep 08, 2010
Posts: 9

It's true that a lot of people avoid GridBagLayout. I created two helper classes, and now use GBL *most* of the time. The two classes aren't all that complex or elegant, which made the whole topic a lot less to worry about.

The first helper class is: GblPanel. It extends JPanel, and everywhere else that I would have used a JPanel, I use my helper class GblPanel.

The second helper class is: Gbc. It extends GridBagConstraints, and simply provides default values for what I normally use and like.

At the end, I have a sample of how I use these classes:




Now, building a panel is as easy as calling "add" on the panel with just about any Swing object and the corresponding x, y position. For controls that need to span, just add two more arguments. For example,

And now for something really cool... For debugging and seeing the grid, call one of the debug methods that I created:

Give it a try. GBL isn't all that bad...

pete stein

Joined: Feb 23, 2007
Posts: 1561
DaveSmith Colorado wrote:
Give it a try. GBL isn't all that bad...


It's evil, I tell you!

But seriously, it definitely does have its uses. Another option that is not part of the core Java classes is MiGLayout.

Nice first post, and please allow me to be the first to welcome you to JavaRanch!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20078

Dave, were you aware of GBC? It looks quite a bit like your Gbc class.

How To Ask Questions How To Answer Questions
David A. Smith

Joined: Sep 08, 2010
Posts: 9

Rob Prime wrote:Dave, were you aware of GBC? It looks quite a bit like your Gbc class.

Yup. I started with that and tweaked it for how I use the other (GblPanel) class...
I agree. Here's the link:
subject: GridBagLayout question
jQuery in Action, 3rd edition