Example usage for org.eclipse.swt.widgets Button setText

List of usage examples for org.eclipse.swt.widgets Button setText

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button setText.

Prototype

public void setText(String text) 

Source Link

Document

Sets the receiver's text.

Usage

From source file:org.eclipse.swt.snippets.Snippet354.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 354");
    shell.setLayout(new GridLayout(1, false));

    Menu appMenuBar = display.getMenuBar();
    if (appMenuBar == null) {
        appMenuBar = new Menu(shell, SWT.BAR);
        shell.setMenuBar(appMenuBar);//from   w  w w.j a  v  a 2 s.c om
    }
    MenuItem file = new MenuItem(appMenuBar, SWT.CASCADE);
    file.setText("File");
    Menu dropdown = new Menu(appMenuBar);
    file.setMenu(dropdown);
    MenuItem exit = new MenuItem(dropdown, SWT.PUSH);
    exit.setText("Exit");
    exit.addSelectionListener(widgetSelectedAdapter(e -> display.dispose()));

    Listener keyDownFilter = event -> System.out.println("Key event!");
    display.addFilter(SWT.KeyDown, keyDownFilter);
    display.addFilter(SWT.KeyUp, keyDownFilter);

    ArmListener armListener = e -> System.out.println(e);

    Menu systemMenu = display.getSystemMenu();
    if (systemMenu != null) {
        systemMenu.addMenuListener(new MenuListener() {
            @Override
            public void menuHidden(MenuEvent e) {
                System.out.println("App menu closed");
            }

            @Override
            public void menuShown(MenuEvent e) {
                System.out.println("App menu opened");
            }
        });

        MenuItem sysItem = getItem(systemMenu, SWT.ID_QUIT);
        sysItem.addArmListener(armListener);
        sysItem.addSelectionListener(widgetSelectedAdapter(e -> System.out.println("Quit selected")));
        sysItem = getItem(systemMenu, SWT.ID_HIDE_OTHERS);
        sysItem.addArmListener(armListener);
        sysItem.addSelectionListener(widgetSelectedAdapter(e -> {
            System.out.println("Hide others selected -- and blocked!");
            e.doit = false;
        }));
        sysItem = getItem(systemMenu, SWT.ID_HIDE);
        sysItem.addArmListener(armListener);
        sysItem.addSelectionListener(widgetSelectedAdapter(e -> {
            System.out.println("Hide selected -- and blocked!");
            e.doit = false;
        }));
        sysItem = getItem(systemMenu, SWT.ID_PREFERENCES);
        sysItem.addArmListener(armListener);
        sysItem.addSelectionListener(widgetSelectedAdapter(e -> System.out.println("Preferences selected")));
        sysItem = getItem(systemMenu, SWT.ID_ABOUT);
        sysItem.addArmListener(armListener);
        sysItem.addSelectionListener(widgetSelectedAdapter(e -> System.out.println("About selected")));

        int prefsIndex = systemMenu.indexOf(getItem(systemMenu, SWT.ID_PREFERENCES));
        MenuItem newAppMenuItem = new MenuItem(systemMenu, SWT.CASCADE, prefsIndex + 1);
        newAppMenuItem.setText("SWT-added item");
        newAppMenuItem.setAccelerator(SWT.MOD1 | 'i');
        newAppMenuItem.addArmListener(armListener);
        newAppMenuItem.addSelectionListener(
                widgetSelectedAdapter(e -> System.out.println("SWT-added item selected")));
        Menu subMenu = new Menu(systemMenu);

        for (int i = 0; i < 4; i++) {
            MenuItem subItem = new MenuItem(subMenu, SWT.PUSH);
            subItem.setText("Item " + i);
        }
        newAppMenuItem.setMenu(subMenu);
    }
    Button b = new Button(shell, SWT.PUSH);
    b.setText("Test");
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet282.java

public static void main(String[] args) {
    final Display display = new Display();
    final Clipboard clipboard = new Clipboard(display);
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setText("Snippet 282");
    shell.setLayout(new GridLayout());
    shell.setText("Clipboard ImageTransfer");

    final Button imageButton = new Button(shell, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.minimumHeight = 400;/*from   w  w w.  j  ava  2s .  c  o  m*/
    gd.minimumWidth = 600;
    imageButton.setLayoutData(gd);

    final Text imageText = new Text(shell, SWT.BORDER);
    imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    Composite buttons = new Composite(shell, SWT.NONE);
    buttons.setLayout(new GridLayout(4, true));
    Button button = new Button(buttons, SWT.PUSH);
    button.setText("Open");
    button.addListener(SWT.Selection, event -> {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setText("Open an image file or cancel");
        String string = dialog.open();
        if (string != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null)
                image.dispose();
            image = new Image(display, string);
            imageButton.setImage(image);
            imageText.setText(string);
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Copy");
    button.addListener(SWT.Selection, event -> {
        Image image = imageButton.getImage();
        if (image != null) {
            ImageTransfer imageTransfer = ImageTransfer.getInstance();
            TextTransfer textTransfer = TextTransfer.getInstance();
            clipboard.setContents(new Object[] { image.getImageData(), imageText.getText() },
                    new Transfer[] { imageTransfer, textTransfer });
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Paste");
    button.addListener(SWT.Selection, event -> {
        ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
        if (imageData != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null)
                image.dispose();
            image = new Image(display, imageData);
            imageButton.setImage(image);
        } else {
            imageButton.setText("No image");
            imageButton.setImage(null);
        }
        String text = (String) clipboard.getContents(TextTransfer.getInstance());
        if (text != null) {
            imageText.setText(text);
        } else {
            imageText.setText("");
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Clear");
    button.addListener(SWT.Selection, event -> {
        imageButton.setText("");
        Image image = imageButton.getImage();
        if (image != null)
            image.dispose();
        imageButton.setImage(null);
        imageText.setText("");
    });

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

From source file:YetAnotherBorderLayout.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new YetAnotherBorderLayout());
    Button b1 = new Button(shell, SWT.PUSH);
    b1.setText("North");
    b1.setLayoutData(BorderData.NORTH);/*w w  w  .ja v a2 s .co  m*/
    Button b2 = new Button(shell, SWT.PUSH);
    b2.setText("South");
    b2.setLayoutData(BorderData.SOUTH);
    Button b3 = new Button(shell, SWT.PUSH);
    b3.setText("East");
    b3.setLayoutData(BorderData.EAST);
    Button b4 = new Button(shell, SWT.PUSH);
    b4.setText("West");
    b4.setLayoutData(BorderData.WEST);
    Button b5 = new Button(shell, SWT.PUSH);
    b5.setText("Center");
    b5.setLayoutData(BorderData.CENTER);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet223.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("ExpandBar Example");
    ExpandBar bar = new ExpandBar(shell, SWT.V_SCROLL);
    Image image = display.getSystemImage(SWT.ICON_QUESTION);

    // First item
    Composite composite = new Composite(bar, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
    layout.verticalSpacing = 10;// ww w  .  j a va2s. c o m
    composite.setLayout(layout);
    Button button = new Button(composite, SWT.PUSH);
    button.setText("SWT.PUSH");
    button = new Button(composite, SWT.RADIO);
    button.setText("SWT.RADIO");
    button = new Button(composite, SWT.CHECK);
    button.setText("SWT.CHECK");
    button = new Button(composite, SWT.TOGGLE);
    button.setText("SWT.TOGGLE");
    ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
    item0.setText("What is your favorite button");
    item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    item0.setControl(composite);
    item0.setImage(image);

    // Second item
    composite = new Composite(bar, SWT.NONE);
    layout = new GridLayout(2, false);
    layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
    layout.verticalSpacing = 10;
    composite.setLayout(layout);
    Label label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_ERROR));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_ERROR");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_INFORMATION");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_WARNING));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_WARNING");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_QUESTION));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_QUESTION");
    ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 1);
    item1.setText("What is your favorite icon");
    item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    item1.setControl(composite);
    item1.setImage(image);

    // Third item
    composite = new Composite(bar, SWT.NONE);
    layout = new GridLayout(2, true);
    layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
    layout.verticalSpacing = 10;
    composite.setLayout(layout);
    label = new Label(composite, SWT.NONE);
    label.setText("Scale");
    new Scale(composite, SWT.NONE);
    label = new Label(composite, SWT.NONE);
    label.setText("Spinner");
    new Spinner(composite, SWT.BORDER);
    label = new Label(composite, SWT.NONE);
    label.setText("Slider");
    new Slider(composite, SWT.NONE);
    ExpandItem item2 = new ExpandItem(bar, SWT.NONE, 2);
    item2.setText("What is your favorite range widget");
    item2.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    item2.setControl(composite);
    item2.setImage(image);

    item1.setExpanded(true);
    bar.setSpacing(8);
    shell.setSize(400, 350);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    image.dispose();
    display.dispose();
}

From source file:GridLayoutSimpleDmo.java

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

    GridLayout gridLayout = new GridLayout(4, false);
    gridLayout.verticalSpacing = 8;//  w  ww.  jav  a 2s  .  c  o m

    shell.setLayout(gridLayout);

    Label label = new Label(shell, SWT.NULL);
    label.setText("Title: ");

    Text title = new Text(shell, SWT.SINGLE | SWT.BORDER);
    GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.horizontalSpan = 3;
    title.setLayoutData(gridData);

    label = new Label(shell, SWT.NULL);
    label.setText("Author(s): ");

    Text authors = new Text(shell, SWT.SINGLE | SWT.BORDER);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.horizontalSpan = 3;
    authors.setLayoutData(gridData);

    label = new Label(shell, SWT.NULL);
    label.setText("Cover: ");

    gridData = new GridData();
    gridData.verticalSpan = 3;
    label.setLayoutData(gridData);

    CLabel cover = new CLabel(shell, SWT.NULL);

    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 1;
    gridData.verticalSpan = 3;
    gridData.heightHint = 100;
    gridData.widthHint = 100;

    cover.setLayoutData(gridData);

    label = new Label(shell, SWT.NULL);
    label.setText("Pages");

    Text pages = new Text(shell, SWT.SINGLE | SWT.BORDER);
    pages.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    label = new Label(shell, SWT.NULL);
    label.setText("Publisher");

    Text publisher = new Text(shell, SWT.SINGLE | SWT.BORDER);
    publisher.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    label = new Label(shell, SWT.NULL);
    label.setText("Rating");

    Combo rating = new Combo(shell, SWT.READ_ONLY);
    rating.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    rating.add("5");
    rating.add("4");
    rating.add("3");
    rating.add("2");
    rating.add("1");

    label = new Label(shell, SWT.NULL);
    label.setText("Abstract:");

    Text bookAbstract = new Text(shell, SWT.WRAP | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
    gridData.horizontalSpan = 3;
    gridData.grabExcessVerticalSpace = true;

    bookAbstract.setLayoutData(gridData);

    Button enter = new Button(shell, SWT.PUSH);
    enter.setText("Enter");

    gridData = new GridData();
    gridData.horizontalSpan = 4;
    gridData.horizontalAlignment = GridData.END;
    enter.setLayoutData(gridData);

    title.setText("Professional Java Interfaces with SWT/JFace");
    authors.setText("Jack Li Guojie");
    pages.setText("500pp");
    publisher.setText("John Wiley & Sons");
    cover.setBackground(new Image(display, "yourFile.gif"));
    bookAbstract.setText("SWT/JFace. ");

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

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

From source file:StyledTextEmbebControls.java

public static void main(String[] args) {
    final Display display = new Display();
    Font font = new Font(display, "Tahoma", 32, SWT.NORMAL);
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setFont(font);/*from  w  w w .  ja va2  s . c o  m*/
    styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    styledText.setText(text);
    controls = new Control[2];
    Button button = new Button(styledText, SWT.PUSH);
    button.setText("Button 1");
    controls[0] = button;
    Combo combo = new Combo(styledText, SWT.NONE);
    combo.add("item 1");
    combo.add("another item");
    controls[1] = combo;
    offsets = new int[controls.length];
    int lastOffset = 0;
    for (int i = 0; i < controls.length; i++) {
        int offset = text.indexOf("\uFFFC", lastOffset);
        offsets[i] = offset;
        addControl(controls[i], offsets[i]);
        lastOffset = offset + 1;
    }

    // use a verify listener to keep the offsets up to date
    styledText.addVerifyListener(new VerifyListener() {
        public void verifyText(VerifyEvent e) {
            int start = e.start;
            int replaceCharCount = e.end - e.start;
            int newCharCount = e.text.length();
            for (int i = 0; i < offsets.length; i++) {
                int offset = offsets[i];
                if (start <= offset && offset < start + replaceCharCount) {
                    // this widget is being deleted from the text
                    if (controls[i] != null && !controls[i].isDisposed()) {
                        controls[i].dispose();
                        controls[i] = null;
                    }
                    offset = -1;
                }
                if (offset != -1 && offset >= start)
                    offset += newCharCount - replaceCharCount;
                offsets[i] = offset;
            }
        }
    });

    // reposition widgets on paint event
    styledText.addPaintObjectListener(new PaintObjectListener() {
        public void paintObject(PaintObjectEvent event) {
            StyleRange style = event.style;
            int start = style.start;
            for (int i = 0; i < offsets.length; i++) {
                int offset = offsets[i];
                if (start == offset) {
                    Point pt = controls[i].getSize();
                    int x = event.x + MARGIN;
                    int y = event.y + event.ascent - 2 * pt.y / 3;
                    controls[i].setLocation(x, y);
                    break;
                }
            }
        }
    });

    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    font.dispose();
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet21.java

public static void main(String[] args) {
    Display display = new Display();
    final Color red = display.getSystemColor(SWT.COLOR_RED);
    final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Shell shell = new Shell(display);
    shell.setText("Snippet 21");
    Button b = new Button(shell, SWT.PUSH);
    Rectangle clientArea = shell.getClientArea();
    b.setBounds(clientArea.x + 10, clientArea.y + 10, 100, 32);
    b.setText("Button");
    shell.setDefaultButton(b);// ww  w.  j av a 2 s  .  co m
    final Canvas c = new Canvas(shell, SWT.BORDER);
    c.setBounds(10, 50, 100, 32);
    c.addListener(SWT.Traverse, e -> {
        switch (e.detail) {
        /* Do tab group traversal */
        case SWT.TRAVERSE_ESCAPE:
        case SWT.TRAVERSE_RETURN:
        case SWT.TRAVERSE_TAB_NEXT:
        case SWT.TRAVERSE_TAB_PREVIOUS:
        case SWT.TRAVERSE_PAGE_NEXT:
        case SWT.TRAVERSE_PAGE_PREVIOUS:
            e.doit = true;
            break;
        }
    });
    c.addListener(SWT.FocusIn, e -> c.setBackground(red));
    c.addListener(SWT.FocusOut, e -> c.setBackground(blue));
    c.addListener(SWT.KeyDown, e -> System.out.println("KEY"));
    Text t = new Text(shell, SWT.SINGLE | SWT.BORDER);
    t.setBounds(10, 85, 100, 32);

    Text r = new Text(shell, SWT.MULTI | SWT.BORDER);
    r.setBounds(10, 120, 100, 32);

    c.setFocus();
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.examples.accessibility.AccessibleTableExample.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    shell.setText("Accessible Table Example");

    Group group = new Group(shell, SWT.NONE);
    group.setText("Tables With Accessible Cell Children");
    group.setLayout(new GridLayout());

    new Label(group, SWT.NONE).setText("CTable with column headers");

    table1 = new CTable(group, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    table1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    table1.setHeaderVisible(true);//  w w w.  java2  s.com
    table1.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        CTableColumn column = new CTableColumn(table1, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int row = 0; row < 4; row++) {
        CTableItem item = new CTableItem(table1, SWT.NONE);
        item.setText(new String[] { "C0R" + row, "C1R" + row, "C2R" + row });
    }

    Composite btnGroup = new Composite(group, SWT.NONE);
    btnGroup.setLayout(new FillLayout(SWT.VERTICAL));
    Button btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Add rows");
    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        int currSize = table1.getItemCount();
        int colCount = table1.getColumnCount();
        CTableItem item = new CTableItem(table1, SWT.NONE);
        String[] cells = new String[colCount];

        for (int i = 0; i < colCount; i++) {
            cells[i] = "C" + i + "R" + currSize;
        }
        item.setText(cells);
    }));
    btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Remove rows");
    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        int currSize = table1.getItemCount();
        if (currSize > 0) {
            table1.remove(currSize - 1);
        }
    }));
    btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Remove selected rows");
    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        CTableItem[] selectedItems = table1.getSelection();
        for (CTableItem selectedItem : selectedItems) {
            selectedItem.dispose();
        }
    }));
    btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Add column");
    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        int currSize = table1.getColumnCount();
        CTableColumn item = new CTableColumn(table1, SWT.NONE);
        item.setText("Col " + currSize);
        item.setWidth(50);
    }));
    btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Remove last column");

    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        int colCount = table1.getColumnCount();
        if (colCount > 0) {
            CTableColumn column = table1.getColumn(colCount - 1);
            column.dispose();
        }
    }));

    new Label(group, SWT.NONE).setText("CTable used as a list");

    CTable table2 = new CTable(group, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    table2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    table2.setLinesVisible(true);
    for (String element : itemText) {
        CTableItem item = new CTableItem(table2, SWT.NONE);
        item.setText(element);
    }

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

From source file:org.eclipse.swt.snippets.Snippet296.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 296");
    shell.setBounds(10, 10, 300, 300);/*w w w .jav a 2  s  .  c  om*/
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.VERTICAL);
    sc.setBounds(10, 10, 280, 200);
    final int clientWidth = sc.getClientArea().width;

    final Tree tree = new Tree(sc, SWT.NONE);
    for (int i = 0; i < 99; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        new TreeItem(item, SWT.NONE).setText("child");
    }
    sc.setContent(tree);
    int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
    tree.setSize(clientWidth, prefHeight);
    /*
     * The following listener ensures that the Tree is always large
     * enough to not need to show its own vertical scrollbar.
     */
    tree.addTreeListener(new TreeListener() {
        @Override
        public void treeExpanded(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }

        @Override
        public void treeCollapsed(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }
    });
    /*
     * The following listener ensures that a newly-selected item
     * in the Tree is always visible.
     */
    tree.addSelectionListener(widgetSelectedAdapter(e -> {
        TreeItem[] selectedItems = tree.getSelection();
        if (selectedItems.length > 0) {
            Rectangle itemRect = selectedItems[0].getBounds();
            Rectangle area = sc.getClientArea();
            Point origin = sc.getOrigin();
            if (itemRect.x < origin.x || itemRect.y < origin.y
                    || itemRect.x + itemRect.width > origin.x + area.width
                    || itemRect.y + itemRect.height > origin.y + area.height) {
                sc.setOrigin(itemRect.x, itemRect.y);
            }
        }
    }));
    /*
     * The following listener scrolls the Tree one item at a time
     * in response to MouseWheel events.
     */
    tree.addListener(SWT.MouseWheel, event -> {
        Point origin = sc.getOrigin();
        if (event.count < 0) {
            origin.y = Math.min(origin.y + tree.getItemHeight(), tree.getSize().y);
        } else {
            origin.y = Math.max(origin.y - tree.getItemHeight(), 0);
        }
        sc.setOrigin(origin);
    });

    Button downButton = new Button(shell, SWT.PUSH);
    downButton.setBounds(10, 220, 120, 30);
    downButton.setText("Down 10px");
    downButton.addListener(SWT.Selection, event -> sc.setOrigin(0, sc.getOrigin().y + 10));
    Button upButton = new Button(shell, SWT.PUSH);
    upButton.setBounds(140, 220, 120, 30);
    upButton.setText("Up 10px");
    upButton.addListener(SWT.Selection, event -> sc.setOrigin(0, sc.getOrigin().y - 10));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:BorderLayoutSWT.java

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

    shell.setLayout(new BorderLayout());

    Button buttonWest = new Button(shell, SWT.PUSH);
    buttonWest.setText("West");
    buttonWest.setLayoutData(new BorderLayout.BorderData(BorderLayout.WEST));

    Button buttonEast = new Button(shell, SWT.PUSH);
    buttonEast.setText("East");
    buttonEast.setLayoutData(new BorderLayout.BorderData(BorderLayout.EAST));

    Button buttonNorth = new Button(shell, SWT.PUSH);
    buttonNorth.setText("North");
    buttonNorth.setLayoutData(new BorderLayout.BorderData(BorderLayout.NORTH));

    Button buttonSouth = new Button(shell, SWT.PUSH);
    buttonSouth.setText("South");
    buttonSouth.setLayoutData(new BorderLayout.BorderData(BorderLayout.SOUTH));

    Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    text.setText("Center");
    text.setLayoutData(new BorderLayout.BorderData(BorderLayout.CENTER));

    shell.pack();/*  ww w .  j a v  a2 s. c o m*/
    shell.open();
    // textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();

}