GroupLayout is a LayoutManager that hierarchically groups components to achieve common, and not so common, layouts. Grouping is done by instances of the Group class. GroupLayout supports two types of groups:
Sequential: | A sequential group positions its child elements sequentially, one after another. |
Parallel: | A parallel group positions its child elements in the same space on top of each other. Parallel groups can also align the child elements along their baseline. |
Each Group can contain any number of child groups, Components or gaps. GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a separate group representing the vertical axis. The horizontal group is responsible for setting the x and width of its contents, where as the vertical group is responsible for setting the y and height of its contents.
The following code builds a simple layout consisting of two labels in one column, followed by two textfields in the next column:
JComponent panel = ...; GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); layout.setAutocreateGaps(true); layout.setAutocreateContainerGaps(true); GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); hGroup.add(layout.createParallelGroup().add(label1).add(label2)). add(layout.createParallelGroup().add(tf1).add(tf2)); layout.setHorizontalGroup(hGroup); GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup(); vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE).add(label1).add(tf1)). add(layout.createParallelGroup(GroupLayout.BASELINE).add(label2).add(tf2)); layout.setVerticalGroup(vGroup);
This layout consists of the following:
- The horizontal axis consists of a sequential group containing two parallel groups. The first parallel group consists of the labels, with the second parallel group consisting of the text fields.
- The vertical axis similarly consists of a sequential group containing two parallel groups. The parallel groups align their contents along the baseline. The first parallel group consists of the first label and text field, and the second group consists of the second label and text field.
There are a couple of things to notice in this code:
- You need not explicitly add the components to the container, this is indirectly done by using one of the
add
methods. - The various
add
methods of Groups
return themselves. This allows for easy chaining of invocations. For example, group.add(label1).add(label2);
is equivalent to group.add(label1);group.add(label2);
. - There are no public constructors for the Groups, instead use the create methods of
GroupLayout
.
GroupLayout offer the ability to automatically insert the appropriate gap between components. This can be turned on using the
setAutocreateGaps()
method. Similarly you can use the
setAutocreateContainerGaps()
method to insert gaps between the components and the container.
@version $Revision: 1.11 $
@author Tomas Pavek
@author Jan Stola
@author Scott Violet