posted 14 years ago
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.