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