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.

5 comments:

Anonymous March 24, 2011 at 1:39 AM  

very explanatory and helpful. thanx

Anonymous March 28, 2011 at 11:21 PM  

Explained in very simple words. Thanks:)

Adnan A.Siddiqi February 10, 2014 at 12:08 AM  

It throws Exception:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.basic.BasicBorders$FieldBorder@1eaefc5,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=1,left=1,bottom=1,right=1],selectedTextColor=javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],selectionColor=javax.swing.plaf.ColorUIResource[r=10,g=36,b=106],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING] is not attached to a vertical group

Anonymous February 16, 2014 at 12:11 PM  

I have been reading Oracles Documentation over and over again, I read a few GUI books with many pages dedicated to all kinds of Layout Managers, hence even Core JFC stuff didn't do it fo me. I watched youtube movies, I browsed many code examples - and still couldn't fully comprehend the Grouplayout. Your explanation is very straightforward and should be adapted by Java book authors and definitely by Oracle!

Anonymous June 12, 2018 at 1:14 PM  

Thank you for explaining how GroupLayout works.
I am having the following java Exception

java.lang.IllegalArgumentException: GroupLayout can only be used with one Container at a time
at javax.swing.GroupLayout.checkParent(GroupLayout.java:1116)
at javax.swing.GroupLayout.invalidateLayout(GroupLayout.java:1008)
at java.awt.Container.invalidate(Container.java:1587)
at java.awt.Component.addNotify(Component.java:6933)
at java.awt.Container.addNotify(Container.java:2764)
at javax.swing.JComponent.addNotify(JComponent.java:4740)
at java.awt.Container.addNotify(Container.java:2775)
at javax.swing.JComponent.addNotify(JComponent.java:4740)
at java.awt.Container.addNotify(Container.java:2775)
at javax.swing.JComponent.addNotify(JComponent.java:4740)
at javax.swing.JRootPane.addNotify(JRootPane.java:757)
at java.awt.Container.addNotify(Container.java:2775)
at java.awt.Window.addNotify(Window.java:770)
at java.awt.Frame.addNotify(Frame.java:487)
at java.awt.Window.show(Window.java:1031)
at java.awt.Component.show(Component.java:1671)
at java.awt.Component.setVisible(Component.java:1623)
at java.awt.Window.setVisible(Window.java:1014)
at edu.mit.ll.oms.appendixCGenGui.GuiGenerator$7.run(GuiGenerator.java:435)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Total Pageviews

Tags

Twitter Updates
    follow me on Twitter

    Followers