April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Constructing SWT Layouts, Page 5

  • April 15, 2004
  • By Koray Guclu, Koray Guclu
  • Send Email »
  • More Articles »

StackLayout

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();
  parent.setLayout(layout);
  
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;
    group[k].setLayout(gridLayout);
    
    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]];
   parent.layout();
    }
  });
  // ...

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.

www.korayguclu.de

Resources

Links

Books





Page 5 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel