Chapter 9. SWT Layouts

Up until this point, the examples have constructed simple user interfaces that consisted of one, or maybe two, widgets, which were placed directly on the Shell. setBounds( ) was used to control the size and positioning of each widget.

Although this approach works well, it does have its deficiencies. If you run one of the earlier examples and resize the window, you will uncover one of these—the widgets remain fixed in location with respect to the upper lefthand corner of the window and remain the same size. Even if the window is resized so as to cover up essential widgets, they remain fixed in position. This is not how windows usually behave, and your users will expect your programs to behave differently. This has been an issue with graphical environments since inception, and is not confined to SWT. Over the years, there have been many solutions.

Layouts were the solution devised by Java and have been a part of the Java SDK since the earliest versions. The SWT also uses the layout approach and the effect is very similar to the layouts found in the Java SDK, with perhaps more ease of use.

What Are the SWT Layouts?

Layouts are a solution that has been part of the Java platform since the earliest versions. A layout controls both placement and sizing of components within a container, according to the rules governing that particular layout type. SWT has four layout types:

  • FillLayout

  • GridLayout

  • RowLayout

  • FormLayout

Choosing the proper layout for a container depends ...

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.