Introduction to GroupLayout in Java Swing

            GroupLayout is a very efficient layout manager developed by the Swing Labs project team and was primarily deployed to meet the GUI builder requirements but since it is heavily reliable, it is widely used in manual coding as well.

           The core design principle of this layout manager is dimension independence which means that it treat the components in horizontal and vertical dimensions differently. And while defining layout in each dimensions, the components are further divided into groups which can be sequential or parallel and can contain gaps, components and other groups. Each component must be defined in each dimension otherwise GroupLayout Exception will be thrown by the compiler.

           There are two types of groups in which these components are packed into: sequential and parallel. In a sequential group, components are placed in a line one after the other along one axis, just like other layouts in which components are placed along the same axis. And in a parallel group, components are placed on top of the other with reference to a common point, which can be either along the horizontal axis (right-aligned) or along the vertical axis (bottom-aligned). That means a sequential group in one dimension would be a parallel group in the other.

So the design objective is to define the components in groups and to combine each dimension. Let’s take a simple example. Consider three components arranged in a line.

A     B     C

First step is to define them in each dimension. In the horizontal dimension, there exists one sequential group and in the vertical dimension, there exists one parallel group aligned along the baseline.

Horizontal Layout = sequential group {A, B, C}

Vertical Layout = parallel group (BASELINE) {A, B, C}

Let’s add one more component,

A      B     C 1

                     C2

Now there is a new parallel group (C1, C2) along the horizontal axis and along the vertical axis, there is a new sequential group (C1, C2)

Horizontal layout = sequential group {A, B, parallel group (LEFT) {C1, C2}}

Vertical layout = sequential group {parallel group (BASELINE) {A, B, C1}, C2}

An extra feature offered by the GroupLayout is Gaps. Gaps can be defined as the space between components in the layout. By default GroupLayout defines a preferred space between components. This default gap can be utilized by calling setAutocreateGaps(true) for the corresponding layout. There is a provision to add gaps as a component in a group by invoking methods like addPrefferedGap() and addContainerGap(). We can also set our component gaps by passing the value as a parameter to these methods. There are three constants defined to specify the gap layout namely RELATED, UNRELATED and INDENT. Related specifies a standard gap between components and unrelated represents a much bigger gap than related. Constant indent represents the horizontal gap between the parallel components where the second component (the bottom one) will be placed with the specified indent.

The actual code for the layout of the above example is as shown below:

GroupLayout gl = new GroupLayou(JPanel);

gl.setHorizontalGroup(layout.createSequentialGroup()

.add(A)

.add(B)

.add(gl.createParallelGroup(GroupLayout.LEADING)

.addC1)

.add(C2)));

layout.setVerticalGroup(layout.createSequentialGroup()

.add(layout.createParallelGroup(GroupLayout.BASELINE)

.add(A)

.add(B)

.add(C1))

.add(C2)

);