Layouts in SWT


Overview

When writing applications in the Standard Widget Toolkit (SWT), you may need to use layouts to give your windows a specific look. A layout controls the position and size of children in a Composite. Layout classes are subclasses of the abstract class Layout. SWT provides several standard layout classes, and you can write custom layout classes.
In SWT, positioning and sizing does not happen automatically. Applications can decide to size and place a Composite's children initially, or in a resize listener. Another option is to specify a layout class to position and size the children. If children are not given a size, they will have zero size and they cannot be seen.
The diagram below illustrates a few general terms that are used when discussing layouts. The Composite (in this case, a TabFolder) has a locationclientArea and trim. The size of the Composite is the size of the clientArea plus the size of the trim. This Composite has two children that are laid out side by side. A Layout is managing the size and position of the children. This Layout allows spacing between the children, and a margin between the children and the edges of the Layout. The size of the Layout is the same as the size of theComposite's clientArea.

Standard Layouts

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));



Comments

Popular posts from this blog

input stream or file to X509Certificate

How to rebase chain of git changes on master ?