Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


MigLayout: Easing the Pain of Swing/SWT Layout Management : Page 2

Get a high-level overview of the MigLayout Swing/SWT layout manager, along with an example that demonstrates its conciseness and power.


WEBINAR: On-Demand

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

Getting Started—In No Time
To begin Swing development with MigLayout, download the latest version (3.5 as of this writing) from the MigLayout website and add miglayout-swing.jar to your classpath (miglayout-swt.jar for SWT).

The Quick Start guide (PDF) on the site provides a brief overview and introduction to MigLayout concepts, which this article covers as well. You also can refer to the MigLayout Cheat Sheet (PDF) for a comprehensive listing of all the available constraints.

I was able to complete the example in this article (a complex screen with dozens of controls and various layout constraints) after just two days of experimenting with the API. After finishing the example, I consider myself an experienced MigLayout user who could complete my next screen in probably half the time it took me to create this one. So, as you can see, the learning curve is quite manageable.

MigLayout Examples
A picture (and a few lines of code) are worth a thousand words, so take a look at this basic example from the MigLayout website to see a full set of code:

JPanel p = new JPanel(new MigLayout("", "[right]")); p.add(new JLabel("General"), "split, span, gaptop 10"); p.add(new JSeparator(), "growx, wrap, gaptop 10"); p.add(new JLabel("Company"), "gap 10"); p.add(new JTextField(""), "span, growx"); p.add(new JLabel("Contact"), "gap 10"); p.add(new JTextField(""), "span, growx, wrap"); p.add(new JLabel("Propeller"),"split, span, gaptop 10"); p.add(new JSeparator(), "growx, wrap, gaptop 10"); p.add(new JLabel("PTI/kW"), "gap 10"); p.add(new JTextField(10), ""); p.add(new JLabel("Power/kW"), "gap 10"); p.add(new JTextField(10), "wrap"); p.add(new JLabel("R/mm"), "gap 10"); p.add(new JTextField(10), "wrap"); p.add(new JLabel("D/mm"), "gap 10"); p.add(new JTextField(10));

As you can see, all of the constraints are passed in as a single string parameter that is composed of multiple intuitive keyword/value combinations separated by commas.

Figure 2 shows the MigLayout example at runtime.

Click to enlarge

Figure 2. The Basic MigLayout Example at Runtime: A picture is worth a thousand words, so take a look at this basic MigLayout example.

Under SWT, the code is just as concise, but each control's constraints are passed by calling its setLayoutData() command instead, for example:

MigLayout layout = new MigLayout("","[grow]"); shell.setLayout(layout); Button button = new Button(shell, SWT.PUSH); button.setText("OK"); button.setLayoutData("grow");

Attached to this article, you will find a set of code samples that replicate most of the examples from the Java Swing Tutorial using solely MigLayout. For additional guidance, I implemented a very complex UI screen (probably similar to one most of you have coded at one point in your career) using pure MigLayout (see Figure 3). This UI is included in the code samples as well.

Click to enlarge

Figure 3. A Complex UI Created Solely in MigLayout: This very complex UI screen was implemented using pure MigLayout.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date