I'm somewhat new to Swing programming, and I find that as much as I love the power of GridBagLayout, if you've got a lot of components, there are lots of lines of code just setting up the constraints for the layout. What are some good ways to keep this under control, besides using a visual editor?
Remember that when you add GridBagConstraints
to a GridBagLayout
the data is copied (actually cloned). Therefore, you don't need to start from scratch for each component you add.
You may find the double-brace idiom useful.
I recommend using a better builder than GridBagConstraints
public fields. See my blog entry on the subject for an example.
I feel your pain. I feel it so much, that I have recently abandoned GridBagLayout entirely, in favor of another tool - JGoodies Forms. Its layout code is more concise, reads at a higher level, and is in general much more satisfying to use than GridBagLayout.
It defines a mini-language to define layouts. It's cryptic at first glance, but don't let that discourage you.
If you are able to use it in your environment, I would highly recommend it.
Yes, GridBagLayout is really painful. But it's still powerful. In many cases, you cannot escape from it. Lets check this tiny utility to see it it ease your pain painless-gridbag