We’re Gonna Go “Layout! Layout!” (That’s where the fun is)

I’ve been doing a non-trivial amount of Swing development for the first time in a good long time, and so I’ve been looking at LayoutManagers for the first time in a while.

First, I was playing with GridBagLayout because, well, that’s what I learned when I was a child programmer. It’s the most flexible of the layout managers but, jeepers, it sure results in a lot of code. Ugly code, too.

So I’ve been looking at some of the newer layout managers. JDK 1.5 added the SpringLayout, which documentation describes as being designed for GUI builders. I’ve never felt a lot of love for this layout manager.

But Java 6 brought in an interesting layout manager called GroupLayout which appears well-designed to handle a number of more complicated layouts. I have only two concerns.

First, I’m on a Java 5 machine (Mac OS X 10.4/Tiger) and don’t have access to Java 6. That problem can be solved in two ways. One: maybe it’s time to upgrade to OS X 10.5/Leopard. Or, I can make use of SwingLabs’ SwingX implementation of GroupLayout (which I think came first). It’s almost identical from an API perspective, and it does the job nicely.

The second concern is that although it seems “easier” to use than GridBadLayout, it still results in a big chunk of butt-ugly code sitting in the middle of a initialize method somewhere.

So recently I found MiGLayout, which some people suggest is the best thing to happen to Swing since its inception. (It’s also a viable layout for SWT, apparently) There’s a move afoot to include MiGLayout in Java 7, so that’s interesting, too.

If you look at the example on the bottom of the MiGLayout website, it’s attractively compact. The “constraints” object is just sequence of keywords (each of which has abbreviated forms). It’s quite tidy.

Also: check out their Java web start demo (JDK 1.4+).

It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

Leave a Reply