Layouts are classes to facilitate the positioning and sizing of controls inside a top-level window. They are platform independent, and do not share the same behavior as widgets.
The use of layouts is not mandatory when developing a SWT application; nonetheless, their use is highly encouraged. Layouts are a way to facilitate managing and positioning of real-world applications controls, because the use of setSize
and setLocation
methods in large programs can be a burden.
Layouts offer some benefits when compared to the positioning by hand. They often have the ability to compute the initial size of controls and to reposition them when a resizing operation occurs.
There are two types of the layout
classes:
A layout data follows an algorithm that makes use of additional information on how the control should be positioned. When using this type of layout, we need to associate the composite with the layout
class, and the composite children with the layout data.
Regardless, each layout presents a different underlying algorithm, and in the following sections we describe the available SWT layouts.
The simplest layout class is FillLayout
, which does not work along with a layout data. It positions the controls in a single row or column using the available space, and divides and assigns the same space for each one. If nothing is specified, it assumes that the controls will be as high as the highest one, and as wide as the widest. FillLayout
is often used when the application has one child, as it will completely fill the shell.
FillLayout
provides the following public fields:
type
: Layout orientation, that can be SWT.HORIZONTAL
or SWT.VERTICAL
marginWidth
: Number of pixels of the margin to be placed along the left and right edges of the layoutmarginHeight
: Number of pixels of the margin to be placed along the top and bottom edges of the layoutspacing
: Number of pixels responsible for the space among controlsWe show an example of how to position 10 elements of a Text widget as follows. Note that when the application is resized, the controls are also resized by FillLayout
.
Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); for (int i=0; i<10; i++) { Text text = new Text(shell, SWT.BORDER); text.setText("Fill" + i); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose();
The output of this code snippet is shown as follows:
The following image shows the resize behavior:
RowLayout
positions the controls in a row (horizontally) or in a column (vertically), depending on how it was instantiated. It differs from FillLayout
, as it does not fill out all the available space and it presents a layout data class called RowData
to configure each of the controls individually. It is possible to specify their height and width when positioning them in the composite.
The following public fields are available:
center
: Specifies whether the control should be centeredfill
: Specifies whether all controls in a row should be of the same height for horizontal layouts or the same width for vertical layoutsjustify
: Specifies if any remaining space should be allocated among the controlsmarginBottom
: Specifies the number of pixels at the bottom edge of the layoutmarginHeight
: Specifies the number of pixels at the top and bottom edges of the layoutmarginLeft
: Specifies the number of pixels at the left edge of the layoutmarginRight
: Specifies the number of pixels at the right edge of the layoutmarginTop
: Specifies the number of pixels at the top edge of the layoutmarginWidth
: Specifies the number of pixels at the left and right edges of the layoutpack
: Specifies whether all controls can take their preferred sizespacing
: Specifies the number of pixels among the edges of the cellstype
: Specifies if the layout is using rows or columnswrap
: Specifies if a control should be wrapped to the next row if there is no space availableRowLayout
can be used to position controls both in columns and in rows. Despite its name, if the programmer sets the field to SWT.VERTICAL
, the layout orientation will follow this pattern.
In the following example, we show how to position a few buttons using RowLayout
and RowData
. In the following example, note that in the later two buttons, we use the RowData
class to specify the size of the buttons by defining the height and the width:
Display display = new Display(); Shell shell = new Shell(display); RowLayout layout = new RowLayout(SWT.HORIZONTAL); layout.wrap = true; layout.fill = false; layout.justify = true; shell.setLayout(layout); Button b = new Button(shell, SWT.PUSH); b.setText("Button 1"); b = new Button(shell, SWT.PUSH); b.setText("Button 2"); RowData data = new RowData(); b.setLayoutData(data); b = new Button(shell, SWT.PUSH); b.setText("150 vertical"); data = new RowData(); data.height = 150; b.setLayoutData(data); b = new Button(shell, SWT.PUSH); b.setText("150 horizontal"); data = new RowData(); data.width = 150; b.setLayoutData(data); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose();
GridLayout
is an excellent option for dealing with layouts. It has several configuration fields and it is possible to organize the controls of a composite by associating each one with a layout data object provided by the GridData
class.
In GridLayout
, it is necessary to specify the number of columns of a grid. The widgets are assigned to the columns in the order they are created, starting from the left and moving to the right. When no more columns are available, the next control is placed in the next row. It provides great flexibility, as each control can also span cells horizontally and vertically.
The following public fields are available for GridLayout
:
horizontalSpacing
: Specifies the number of pixels among the horizontal edges of a cellmakeColumnsEqualWidth
: Specifies whether all the columns need to have the same widthmarginBottom
: Specifies the number of pixels in the vertical margin of the bottom edge of a cellmarginHeight
: Specifies the number of pixels in the vertical margin of the bottom and top edges of a cellmarginLeft
: Specifies the number of pixels in the horizontal margin of the left edge of a cellmarginRight
: Specifies the number of pixels in the horizontal margin of the right edge of a cellmarginTop
: Specifies the number of pixels in the vertical margin of the top edge of a cellmarginWidth
: Specifies the number of pixels in the horizontal margin of the left and right edges of a cellnumColumns
: Specifies the number of cell columns in the Layout
widgetverticalSpacing
: Specifies the number of pixels among the vertical edges of a cellWhen applying GridLayout
to position the elements of an application, it is necessary to create a GridData
object to provide further information on how this positioning will be performed. We show the main public fields that are available for the GridData
class:
verticalAlignment
: Specifies the vertical alignmentWe show an example of how to use GridLayout
and GridData
as follows:
Display display = new Display (); Shell shell = new Shell (display); GridLayout layout = new GridLayout(4, false); shell.setLayout(layout); Button b = new Button(shell, SWT.PUSH); b.setText("LEFT, TOP"); b.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1)); b = new Button(shell, SWT.PUSH); b.setText("LEFT, CENTER"); b.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1)); b = new Button(shell, SWT.PUSH); b.setText("LEFT, BOTTOM"); b.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, true, 1, 1)); b = new Button(shell, SWT.PUSH); b.setText("LEFT, FILL"); b.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, true, 1, 1)); b = new Button(shell, SWT.PUSH); b.setText("FILL, TOP"); b.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1)); b = new Button(shell, SWT.PUSH); b.setText("FILL, CENTER"); b.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1)); b = new Button(shell, SWT.PUSH); b.setText("FILL, BOTTOM"); b.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, true, 1, 1)); b = new Button(shell, SWT.PUSH); b.setText("FILL, FILL"); b.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); shell.open (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose ();
It is possible to realize in this example that GridLayout
is receiving two parameters. The first one is the number of columns, and the second one is to specify that the columns do not have the same width. In a similar fashion, the GridData
method receives six parameters: horizontalAlignment
, verticalAlignment
, grabExcessHorizontalSpace
, grabExcessVerticalSpace
, horizontalSpan
, and verticalSpan
, which were explained previously.
The following GUI output is generated by our example: