thorough understanding of Layout Managers is essential to building complex user interfaces in Java applications. The Standard Widget Toolkit (SWT) provides five different layout managers. This article uses simple examples to introduce all of them (see Figure 1
). It also provides a detailed example for GridLayout
, a very useful, flexible manager that builds screens for frequent resizing.
This layout is very similar to the Swing's GridLayout. It does not honor the width and height of the components placed in itthe child widgets get the entire available space. When you add multiple components, each component gets the same height and width. Unlike GridLayout, FillLayout provides two parameters, HORIZONTAL and VERTICAL, which specify how the components will be placed.
|Figure 1. The Layout Managers: This shows the SWT layout managers class hierarchy.|
RowLayout is analogous to Swing's FlowLayout. This layout does honor the width and height of its child components. Unlike FlowLayout, where the child components are placed from left to right, RowLayout allows you to position components from top to bottom as well. Use the following additional properties to fine tune the position of the child components within the layout:
marginLeft, marginTop, marginRight, marginBottom
Use the Pack
property to set the components with equal width. Use the Justify
to position the widgets across the available space within the composite.
When the number of components you've added to RowLayout exceeds available width, RowLayout wraps the components into the next row (like FlowLayout). Set each components' width and height using the width and height properties.