Introduction to SimpleLayout

Swing comes with a bunch of layout managers. One can create neatly complex user interfaces using these layout managers. The downside is that they are either designed for very special purpose (like BorderLayout), or rather complex (like GridBagLayout). So it takes quite an amount of time to create even simple forms.

This is the point where SimpleLayout comes in. It's designed to be simple to use, but powerful enough to create 80% of the forms of a typical Swing GUI. It uses a grid or table like structure to arrange components. You just have to specify the number of columns, and then you add your components. That's it. No dealing with constraints. Space distribution is based on minimum, preferred and maximum dimension of the components. Components are aligned in the top left corner by default. You can specify it alternatively on a per column basis.

For details, see the Javadoc


// create panel
JPanel panel = new JPanel();
// add layout manager with two columns and a gap of 5 pixels
SimpleLayout layout = new SimpleLayout(2, 5, 5);
// align the first column right and centered
layout.setAlignment(0, SimpleLayout.EAST);

// add a few rows with labels and text fields
panel.add(new JLabel("First name"));
panel.add(new JTextField());
panel.add(new JLabel("Last name"));
panel.add(new JTextField());
panel.add(new JLabel("E-Mail"));
panel.add(new JTextField(20));

This will create the following panel (without border):

First name
Last name

After resizing, the panel will look like this:

First name
Last name

That's it. Imagine coding the same layout with GridBagLayout. This would require much effort, including handling with GridBagConstraints. Logo