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();
label.setText("JLabel");
label.setBorder(null);
JTextField jtfValue = new javax.swing.JTextField();
jtfValue.setText("JTextField");

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

//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
hParallelGroup1.addComponent(label);
hParallelGroup2.addComponent(textField);

//add two parallel groups sequentially in the horizontal sequential group
horizontalSeqGrp.addGroup(hParallelGroup1);
horizontalSeqGrp.addGroup(hParallelGroup2);

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

//add the components
vparallelGroup1.addComponent(label);
vparallelGroup1.addComponent(textField);

//add this parallel group in the vertical sequential group
verticalSeqGrp.addGroup(vparallelGroup1);

//finally set the both sequential group to the grpLayout object
grpLayout.setHorizontalGroup(horizontalSeqGrp);
grpLayout.setVerticalGroup(verticalSeqGrp);


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();
label2.setText("JLabel2");
label2.setBorder(null);
GroupLayout.ParallelGroup hParallelGroup3 = lout.createParallelGroup(GroupLayout.Alignment.LEADING);
hParallelGroup3.addComponent(label2);

//add the new parallel group to the horizontal sequential group
horizontalSeqGrp.addGroup(hParallelGroup3);
vparallelGroup1.addComponent(labe2l);


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
hParallelGroup1.addComponent(label2);

//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
vparallelGroup2.addComponent(label2);

//add the new parallel group to the vertical sequential group
verticalSeqGrp.addGroup(vparallelGroup2);


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

Total Pageviews

Tags

Twitter Updates
    follow me on Twitter

    Followers