Using StackLayouts : StackLayout « SWT « Java Tutorial






To set a particular control on the top, you should first set the topControl field of the StackLayout with that control and then call the layout method of the parent composite.

Using StackLayouts
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class StackLayoutButtons {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);

    final StackLayout stackLayout = new StackLayout();
    shell.setLayout(stackLayout);

    final Button[] buttons = new Button[3];

    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new Button(shell, SWT.NULL);
      buttons[i].setText("Button #" + i);

      buttons[i].addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
          // Flip to next button.
          Button nextButton = null;
          for (int i = 0; i < buttons.length; i++) {
            if (buttons[i] == e.widget) {
              if (i == buttons.length - 1)
                nextButton = buttons[0];
              else
                nextButton = buttons[i + 1];
            }
          }
          stackLayout.topControl = nextButton;
          shell.layout();
        }

        public void widgetDefaultSelected(SelectionEvent e) {
        }
      });
    }

    stackLayout.topControl = buttons[0];

    shell.setSize(450, 400);
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}








17.98.StackLayout
17.98.1.Using StackLayoutsUsing StackLayouts
17.98.2.Using StackLayoutUsing StackLayout
17.98.3.Use a StackLayout to switch between CompositesUse a StackLayout to switch between Composites
17.98.4.Configure the margin: marginHeight and marginWidthConfigure the margin: marginHeight and marginWidth