• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GridBagLayout question

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread would sit better on the Swing forum. Moving.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 9
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave, were you aware of GBC? It looks quite a bit like your Gbc class.
 
David A. Smith
Greenhorn
Posts: 9
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic