ne of the pain points of both Swing and SWT UI development is the tediousness of coding layout constraints, in particular the more complex ones. With Swing in particular, Java programmers have suffered for many years under the crushing weight of GridBagLayout and its extreme verbosity (not to mention complexity). The new GroupLayout in JDK6 has eased this pain somewhat, but only if you use a UI designer that supports it (such as NetBeans Matisse or SWT Designer from Instantiations). Writing the GroupLayout code by hand is nearly impossible for a mere mortal, because it is so complex and unintuitive. And maintaining it manually is even worse; even the slightest change in layout potentially requires a lot of code changes.
Hence, for many years the only viable option for those who preferred to code their layout by hand (or for whom UI designers were just not powerful enough) was the excellent open-source FormsLayout, (created by Karsten Lentzsch).
Today, a new solution called MigLayout is available. Not only does MigLayout improve on all its predecessors, but in my assessment, it simply renders nearly every one of them obsolete. The only base layout manager that MigLayout cannot replace is FlowLayout, because FlowLayout is free-flow oriented and not table/row/column based. Also, although MigLayout can replace BoxLayout, the difference in lines of code is hardly noticeable, because BoxLayout already is such a simple layout manager.
This article provides a high-level overview of the MigLayout Swing/SWT layout manager and provides an example to demonstrate its power. While MigLayout is the only layout manager I know of that works across both Swing and SWT (different implementation classes but the same constraints API across both UI toolkits), this article focuses mostly on the Swing implementation.
The Power of MigLayout
MigLayout is an open-source (BSD license) layout manager created by Mikael Grev. At the time of this writing, it was at version 3.5. Believe it or not, Grev's day job is fighter pilot for the Swedish Air Force. He actually flies a JAS 39 Gripen supersonic jet every day (see Figure 1). But obviously that is not thrilling enough, so he has entered the exciting world of Java open source development.
|Figure 1. Mikael Grev's Boring Day Job: Mikael Grev's day job is fighter pilot for the Swedish Air Force.|
MigLayout's strength lies in the conciseness of its required code. In general, you need one line of code to instantiate a MigLayout with all of its general/column/row constraints and then one line of code per control to define its constraints. So, if you have 50 controls on a frame (an extreme example), you will usually need 51 lines of code to fully configure the layout. The equivalent GridBagLayout or GroupLayout code would probably run into the hundreds, if not thousands, of lines.
As I was using MigLayout to complete a complex UI, I also realized how easy it is to maintain and modify existing screens. It's a matter of simply updating one line of code or moving it up or down in relation to other lines. Even when using an advanced UI designer such as NetBeans's Matisse, it is easy to mix up or corrupt an existing complex layout, especially if you need to move existing controls around or insert new ones. With MigLayout, even in pure code without a UI designer, modifying layouts is a fairly trivial task.