Understanding GroupLayout in Java

GroupLayout is one of the most effective layout managers in Java to place the GUI elements in a very organized way. Here I show you how to use Group Layout to build your GUI.

GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently and it uses two types of arrangements -- sequential and parallel, combined with hierarchical composition. Usually, components placed in parallel in one dimension are in a sequence in the other, so that they do not overlap.

Let’s start with a very simple example to understand this layout.

Suppose you have to put one JLabel and one JTextField component in a row within a container named jpTestGroupLayout

You have to put these comonents both in a Horizontal sequential group and in a Vertical sequential group. But how?

To add the components in a horizontal sequential group at first you need to create two parallel groups. Add the JLable and the JTextField in the parallel groups then add these groups sequentially in the Horizontal sequential group.

And to add the components in a Vertical sequential group, create one parallel group and add the components in the group. Then add this parallel group to your vertical sequential group.

Now let’s look at the code to create the layout described above.

//declare the two component
JLabel label = new javax.swing.JLabel();
JTextField jtfValue = new javax.swing.JTextField();

//create a GroupLayout object associate with the panel
GroupLayout grpLayout = new GroupLayout(jpTestGroupLayout); jpTestGroupLayout.setLayout(grpLayout);
grpLayout.setAutoCreateGaps(true); // specify automatic gap insertion

//create the Horizontal and Vertical and sequential group
GroupLayout.SequentialGroup horizontalSeqGrp = grpLayout.createSequentialGroup();
GroupLayout.SequentialGroup verticalSeqGrp = grpLayout.createSequentialGroup();

//create two parallel group for adding the components in the horizontal sequential group
GroupLayout.ParallelGroup hParallelGroup1 = grpLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
GroupLayout.ParallelGroup hParallelGroup2 = grpLayout.createParallelGroup(GroupLayout.Alignment.LEADING);

//add the components

//add two parallel groups sequentially in the horizontal sequential group

//create one parallel group for adding the components in the vertical sequential group
GroupLayout.ParallelGroup vparallelGroup1 = grpLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);

//add the components

//add this parallel group in the vertical sequential group

//finally set the both sequential group to the grpLayout object

If you want to add another JLabel in the same row then what will you do?

It is very simple now. Just create another parallel group to add the new JLable in the Horizontal sequential group and to add this to the Vertical sequential group add this in the existing vertical parallel group.

JLabel label2 = new javax.swing.JLabel();
GroupLayout.ParallelGroup hParallelGroup3 = lout.createParallelGroup(GroupLayout.Alignment.LEADING);

//add the new parallel group to the horizontal sequential group

Now I describe another scenario. Suppose you want to add the second JLable in the following way.

The code will be like this

//add the new component in the hParallelGroup1

//create a new parallel group to add the new component in the vertical Sequential group
GroupLayout.ParallelGroup vparallelGroup2 = grpLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);

//add the component to the new group

//add the new parallel group to the vertical sequential group

Try the above examples yourself and definitely you will find this layout very easy to manage. For more information visit this link.

Total Pageviews


Twitter Updates
    follow me on Twitter