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(int x, int y) 

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.Snippet294.java

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

    final Shell shell = new Shell(display);
    shell.setText("Regions on a Control");
    shell.setLayout(new FillLayout());
    shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_RED));

    Button b2 = new Button(shell, SWT.PUSH);
    b2.setText("Button with Regions");

    // define a region that looks like a circle with two holes in ot
    Region region = new Region();
    region.add(circle(67, 87, 77));//from  w w w  . j  a  v a 2  s. co  m
    region.subtract(circle(20, 87, 47));
    region.subtract(circle(20, 87, 113));

    // define the shape of the button using setRegion
    b2.setRegion(region);
    b2.setLocation(100, 50);

    b2.addListener(SWT.Selection, e -> shell.close());

    shell.setSize(200, 200);
    shell.open();

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Embedding objects in text");
    final Image[] images = { new Image(display, 32, 32), new Image(display, 20, 40),
            new Image(display, 40, 20) };
    int[] colors = { SWT.COLOR_BLUE, SWT.COLOR_MAGENTA, SWT.COLOR_GREEN };
    for (int i = 0; i < images.length; i++) {
        GC gc = new GC(images[i]);
        gc.setBackground(display.getSystemColor(colors[i]));
        gc.fillRectangle(images[i].getBounds());
        gc.dispose();//  w  w  w . jav a 2s .  co m
    }

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.pack();
    String text = "Here is some text with a blue image \uFFFC, a magenta image \uFFFC, a green image \uFFFC, and a button: \uFFFC.";
    final int[] imageOffsets = { 36, 55, 72 };
    final TextLayout layout = new TextLayout(display);
    layout.setText(text);
    for (int i = 0; i < images.length; i++) {
        Rectangle bounds = images[i].getBounds();
        TextStyle imageStyle = new TextStyle(null, null, null);
        imageStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
        layout.setStyle(imageStyle, imageOffsets[i], imageOffsets[i]);
    }
    Rectangle bounds = button.getBounds();
    TextStyle buttonStyle = new TextStyle(null, null, null);
    buttonStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
    final int buttonOffset = text.length() - 2;
    layout.setStyle(buttonStyle, buttonOffset, buttonOffset);

    shell.addListener(SWT.Paint, event -> {
        GC gc = event.gc;
        Point margin = new Point(10, 10);
        layout.setWidth(shell.getClientArea().width - 2 * margin.x);
        layout.draw(event.gc, margin.x, margin.y);
        for (int i = 0; i < images.length; i++) {
            int offset = imageOffsets[i];
            int lineIndex1 = layout.getLineIndex(offset);
            FontMetrics lineMetrics1 = layout.getLineMetrics(lineIndex1);
            Point point1 = layout.getLocation(offset, false);
            GlyphMetrics glyphMetrics1 = layout.getStyle(offset).metrics;
            gc.drawImage(images[i], point1.x + margin.x,
                    point1.y + margin.y + lineMetrics1.getAscent() - glyphMetrics1.ascent);
        }
        int lineIndex2 = layout.getLineIndex(buttonOffset);
        FontMetrics lineMetrics2 = layout.getLineMetrics(lineIndex2);
        Point point2 = layout.getLocation(buttonOffset, false);
        GlyphMetrics glyphMetrics2 = layout.getStyle(buttonOffset).metrics;
        button.setLocation(point2.x + margin.x,
                point2.y + margin.y + lineMetrics2.getAscent() - glyphMetrics2.ascent);
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    layout.dispose();
    for (int i = 0; i < images.length; i++) {
        images[i].dispose();
    }
    display.dispose();
}

From source file:TextLayoutImageControl.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Embedding objects in text");
    final Image[] images = { new Image(display, 32, 32), new Image(display, 20, 40),
            new Image(display, 40, 20) };
    int[] colors = { SWT.COLOR_BLUE, SWT.COLOR_MAGENTA, SWT.COLOR_GREEN };
    for (int i = 0; i < images.length; i++) {
        GC gc = new GC(images[i]);
        gc.setBackground(display.getSystemColor(colors[i]));
        gc.fillRectangle(images[i].getBounds());
        gc.dispose();/*from  w  w w .j a  v a 2  s . c o  m*/
    }

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.pack();
    String text = "Here is some text with a blue image \uFFFC, a magenta image \uFFFC, a green image \uFFFC, and a button: \uFFFC.";
    final int[] imageOffsets = { 36, 55, 72 };
    final TextLayout layout = new TextLayout(display);
    layout.setText(text);
    for (int i = 0; i < images.length; i++) {
        Rectangle bounds = images[i].getBounds();
        TextStyle imageStyle = new TextStyle(null, null, null);
        imageStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
        layout.setStyle(imageStyle, imageOffsets[i], imageOffsets[i]);
    }
    Rectangle bounds = button.getBounds();
    TextStyle buttonStyle = new TextStyle(null, null, null);
    buttonStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
    final int buttonOffset = text.length() - 2;
    layout.setStyle(buttonStyle, buttonOffset, buttonOffset);

    shell.addListener(SWT.Paint, new Listener() {
        public void handleEvent(Event event) {
            GC gc = event.gc;
            Point margin = new Point(10, 10);
            layout.setWidth(shell.getClientArea().width - 2 * margin.x);
            layout.draw(event.gc, margin.x, margin.y);
            for (int i = 0; i < images.length; i++) {
                int offset = imageOffsets[i];
                int lineIndex = layout.getLineIndex(offset);
                FontMetrics lineMetrics = layout.getLineMetrics(lineIndex);
                Point point = layout.getLocation(offset, false);
                GlyphMetrics glyphMetrics = layout.getStyle(offset).metrics;
                gc.drawImage(images[i], point.x + margin.x,
                        point.y + margin.y + lineMetrics.getAscent() - glyphMetrics.ascent);
            }
            int lineIndex = layout.getLineIndex(buttonOffset);
            FontMetrics lineMetrics = layout.getLineMetrics(lineIndex);
            Point point = layout.getLocation(buttonOffset, false);
            GlyphMetrics glyphMetrics = layout.getStyle(buttonOffset).metrics;
            button.setLocation(point.x + margin.x,
                    point.y + margin.y + lineMetrics.getAscent() - glyphMetrics.ascent);
        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    layout.dispose();
    for (int i = 0; i < images.length; i++) {
        images[i].dispose();
    }
    display.dispose();
}

From source file:CompViewer.java

public Ch3_Group(Composite parent) {
    super(parent, SWT.NONE);
    Group group = new Group(this, SWT.SHADOW_ETCHED_IN);
    group.setText("Group Label");

    Label label = new Label(group, SWT.NONE);
    label.setText("Two buttons:");
    label.setLocation(20, 20);/*from  w  w  w. j ava  2 s .  co  m*/
    label.pack();

    Button button1 = new Button(group, SWT.PUSH);
    button1.setText("Push button");
    button1.setLocation(20, 45);
    button1.pack();

    Button button2 = new Button(group, SWT.CHECK);
    button2.setText("Check button");
    button2.setBounds(20, 75, 90, 30);
    group.pack();
}