permaculture playing cards*
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
Author

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
Sheriff

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

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
Greenhorn

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:

GblPanel:


Gbc:


Sample:


Description:
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...

Dave.
pete stein
Bartender

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

Dave.


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
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David A. Smith
Greenhorn

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...
 
GeeCON Prague 2014
 
subject: GridBagLayout question