Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing" Watch "Swing" New topic


Rakesh Rajagopalan
Ranch Hand

Joined: Sep 19, 2006
Posts: 59
How to use the 'Grid Bag Layout'? Can anyone give me some sample program on that?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> How to use the 'Grid Bag Layout'?

lots of experimentation, trial and error, late nights, coffee, whisky, v_alium.

start with sun's tutorial

there are better ones via google, but I've yet to come across one that explains it easily.

[edit] v_alium is censored??, learn something every day
[ September 20, 2006: Message edited by: Michael Dunn ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Be sure to watch this training video.

[Jess in Action][AskingGoodQuestions]
Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
Here is a very great tool that will make you understand the GridBagLayout thoroughly

This is a free tool and i have uploaded it to rapidshare
Hope it works fine

Life is the biggest school
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
This comes up regularly on these fora.
The hard part to understand about GridBag is not the layout, but the constraints. The constraints are:
  • gridX which means column, starting with 0=left, and
  • gridY which means row, 0=top,
  • gridwidth, how many columns covered
  • gridheight, how many rows etc.
  • weightx, how much it expands left-to-right,
  • weighty ditto up and down
  • anchor, which if any side or corner of its space the component clings to
  • fill, whether it fills its space horizontally, vertically, or both ways
  • Insets, an object representing the spacing from the edge of its area
  • ipadx, internal padding between the spaces left-to-right (I think) and last but not least,
  • ipady, ditto up and down.
  • Now if you use GridBagConstraints, you have to write myConstraints.gridx = 123;
    myConstraints.gridy = 234; every time you add a Component. So your consstraints takes up as much space as the rest of the coding. Find a copy of Horstmann & Cornell's book
    C Horstmann, G Cornell, Core Java 2, 7/e, Vol I-Fundamentals, Santa Clara CA: Sun Microsystems Press (Prentice-Hall) (2004), pages 430ff. Find the class they call GBC.
    You can now add things like this:-
    You can download a copy of the GBC class from Horstmann and Cornell here.
    And I like the tool which Khaled Mahmoud quoted.
    I agree. Here's the link:
    subject: Swing
    It's not a secret anymore!