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

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

Introduction

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

Prototype

public void setLocation(Point location) 

Source Link

Document

Sets the receiver's location to the point specified by the arguments which are relative to the receiver's parent (or its display if its parent is null), unless the receiver is a shell.

Usage

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

public static void main(String[] args) {
    final Display display = new Display();
    Font font = new Font(display, "Tahoma", 16, SWT.NORMAL);
    final Shell shell = new Shell(display);
    shell.setText("Snippet 217");
    shell.setLayout(new GridLayout());
    styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setFont(font);/*from w ww.  j  a  va 2 s  .  com*/
    styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    styledText.setText(text);
    Button button = new Button(styledText, SWT.PUSH);
    button.setText("Button 1");
    int offset = text.indexOf('\uFFFC');
    addControl(button, offset);
    button.setLocation(styledText.getLocationAtOffset(offset));
    Combo combo = new Combo(styledText, SWT.NONE);
    combo.add("item 1");
    combo.add("another item");
    combo.setText(combo.getItem(0));
    offset = text.indexOf('\uFFFC', offset + 1);
    addControl(combo, offset);
    combo.setLocation(styledText.getLocationAtOffset(offset));

    // use a verify listener to dispose the controls
    styledText.addVerifyListener(event -> {
        if (event.start == event.end)
            return;
        String text = styledText.getText(event.start, event.end - 1);
        int index = text.indexOf('\uFFFC');
        while (index != -1) {
            StyleRange style = styledText.getStyleRangeAtOffset(event.start + index);
            if (style != null) {
                Control control = (Control) style.data;
                if (control != null)
                    control.dispose();
            }
            index = text.indexOf('\uFFFC', index + 1);
        }
    });

    // reposition widgets on paint event
    styledText.addPaintObjectListener(event -> {
        Control control = (Control) event.style.data;
        Point pt = control.getSize();
        int x = event.x + MARGIN;
        int y = event.y + event.ascent - 2 * pt.y / 3;
        control.setLocation(x, y);
    });

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