February 27, 2021
Hot Topics:

Constructing SWT Layouts

  • By Koray Guclu
  • Send Email »
  • More Articles »


StackLayout is different from the other layout classes. StackLayout displays only one Control at a time; however, other layout classes attempt to display many Controls at a time. StackLayout is used in property pages, wizards, and so forth.

The StackLayout has the properties listed in Table 9.

Figure 16. StackLayout Example

Figure 16 shows a simple stack layout example. We click on the Show Next Group object each time to change the top control.

Figure 17. StackLayout Example

Figure 17 shows an example of StackLayout. A stack of controls exists. It is allowed to display only one control at a time. The highlighted picture on the right shows the topControl (active) control.

 Source 8.  StackLayout example
  // ...
  shell.setLayout(new GridLayout());
  final Composite parent = new Composite(shell, SWT.NONE);
  parent.setLayoutData(new GridData());
  final StackLayout layout = new StackLayout();
final Group[] group = new Group[3];
  for (int k = 0; k < group.length; k++) 
    group[k] = new Group(parent, SWT.NONE);
    group[k].setText("Group " + (k + 1));

    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 4;
    Character character = new Character((char) ('A' + k));
    for (int i = 10; i < 20; i++)
      Button bArray = new Button(group[k], SWT.PUSH);
      bArray.setText(character + "." + i);
layout.topControl = group[0];
  Button b = new Button(shell, SWT.PUSH);
  b.setText("Show Next Group");
  final int[] index = new int[1];
  b.addListener(SWT.Selection, new Listener() 
    public void handleEvent(Event e) 
      index[0]          = (index[0] + 1) % 3;
   layout.topControl = group[index[0]];
  // ...

We have three groups and we add button controls to those groups. Afterwards, we need to set the initial topControl. We create an action listener when the button is clicked. The action listener changes the top control and calls the layout() method of the parent control. This step is very important: If you do not call the layout method of the parent control, you won't be able to see any change.

About the Author

Koray Güclü

He is working as a freelance author and software architect. He is currently finishing his book on Software Architectures and Design Patterns. His main interest areas are Software Architectures, Data Warehouses, and Database Modeling.





Page 5 of 5

This article was originally published on April 15, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date