Free since 2005 · No login required
AT

Academic Tutorials

Learn at your own pace

site-mobile-top-banner · 320x50

Standard Layouts in SWT Library

Added 31 Jul 2008

The standard layout classes in the SWT library are:

  • FillLayout lays out equal-sized widgets in a single row or column
  • RowLayout lays out widgets in a row or rows, with fill, wrap, and spacing options
  • GridLayout lays out widgets in a grid
  • FormLayout lays out widgets by creating attachments for each of their sides

To use the standard layouts, you need to import the SWT layout package:

import org.eclipse.swt.layout.*;

Layouts are pluggable. To set a Composite widget's layout, you use the widget's setLayout(Layout) method. In the following code, a Shell (a subclass of Composite) is told to position its children using a RowLayout:

Shell shell = new Shell();
shell.setLayout(new RowLayout());

A layout class may have a corresponding layout data class: a subclass of Object that contains layout data for a specific child. By convention, layout data classes are identified by substituting "Data" for "Layout" in the class name. For example, the standard layout class RowLayout has a layout data class called RowData, the layout class GridLayout uses a layout data class called GridData, and the layout class FormLayout has a layout data class called FormData. A widget's layout data class is set as follows:

Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new RowData(50, 40));