o7planning

Java SWT SashForm Tutorial with Examples

  1. SWT SashForm
  2. SashForm Example
  3. SashForm and WindowBuilder

1. SWT SashForm

SashForm is a class extended from Composite, it lays out child components on a row or a column. And it sets a Sash lying between 2 side-by-side child-components. The user can use mouse to change the position of Sash.
The example below is a horizontal SashForm with 3 child-compents, the user can use mouse to move the Sashes.
For example, a vertical SashForm with 4 children.
For horizontal SashForm, the width of subcomponents is directly proportional to their weight. For vertical SashForm, the height of subcomponents is directly proportional to their weight.
// Create horizontal SashForm:
SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);

// Create Vertical SashForm.
SashForm sashForm = new SashForm(shell, SWT.VERTICAL);

2. SashForm Example

SashFormDemo.java
package org.o7planning.swt.sashform;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SashFormDemo {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT SashForm (o7planning.org)");
        FillLayout fillLayout = new FillLayout();
        fillLayout.marginHeight= 5;
        fillLayout.marginWidth = 5;
        shell.setLayout(fillLayout);

        SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);

        Button button1 = new Button(sashForm, SWT.NONE);
        button1.setText("Button 1");
        Button button2 = new Button(sashForm, SWT.NONE);
        button2.setText("Button 2");
        Button button3 = new Button(sashForm, SWT.NONE);
        button3.setText("Button 3");
        sashForm.setWeights(new int[] { 2, 3, 1 });
        sashForm.setSashWidth(10);

        shell.setSize(400, 250);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
Running the example:

3. SashForm and WindowBuilder

Design SashForm with WindowBuilder.