tags:

views:

135

answers:

3

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?

+2  A: 

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.

Tom Hawtin - tackline
+2  A: 

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.

John O
I checked it out - really great. A huge improvement over GBL.
slide_rule
You may also check out DesignGridLayout (http://designgridlayout.dev.java.net) which does the same as JGoodies forms but exclusively through its API, which prevents any possible problem in runtime (if it compiles, it will run correctly!)
jfpoilpret
+1  A: 

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

Tri.Bao