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

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

Introduction

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

Prototype

public void setBounds(int x, int y, int width, int height) 

Source Link

Document

Sets the receiver's size and location to the rectangular area specified by the arguments.

Usage

From source file:DialogClass.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Dialog Example");
    shell.setSize(300, 200);//from   w w w  . j a va  2  s. co  m
    shell.open();

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Delete File");
    button.setBounds(20, 40, 80, 25);

    final Text text = new Text(shell, SWT.SHADOW_IN);
    text.setBounds(140, 40, 100, 25);

    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
    dialog.setText("Delete File");
    dialog.setSize(250, 150);

    final Button buttonOK = new Button(dialog, SWT.PUSH);
    buttonOK.setText("OK");
    buttonOK.setBounds(20, 55, 80, 25);

    Button buttonCancel = new Button(dialog, SWT.PUSH);
    buttonCancel.setText("Cancel");
    buttonCancel.setBounds(120, 55, 80, 25);

    final Label label = new Label(dialog, SWT.NONE);
    label.setText("Delete the file?");
    label.setBounds(20, 15, 100, 20);

    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            if (event.widget == buttonOK) {
                deleteFlag = true;
            } else {
                deleteFlag = false;
            }
            dialog.close();
        }
    };

    buttonOK.addListener(SWT.Selection, listener);
    buttonCancel.addListener(SWT.Selection, listener);

    Listener buttonListener = new Listener() {
        public void handleEvent(Event event) {
            dialog.open();
        }
    };

    button.addListener(SWT.Selection, buttonListener);

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

    if (deleteFlag) {
        text.setText("File deleted.");
    } else {
        text.setText("File not deleted.");
    }

    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);//from  w  w w  .  j  a  v  a2  s.  co m
    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:org.eclipse.swt.snippets.Snippet322.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 322");
    shell.setBounds(10, 10, 300, 300);/*from w w w .  j a v  a 2 s .com*/
    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 disableButton = new Button(shell, SWT.PUSH);
    disableButton.setBounds(10, 220, 120, 30);
    disableButton.setText("Disable");
    disableButton.addListener(SWT.Selection, event -> tree.setEnabled(false));
    Button enableButton = new Button(shell, SWT.PUSH);
    enableButton.setBounds(140, 220, 120, 30);
    enableButton.setText("Enable");
    enableButton.addListener(SWT.Selection, event -> tree.setEnabled(true));

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

From source file:SWTButtonAction.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(200, 200);/*  ww w .  jav a  2  s.c o m*/
    shell.setText("Dialogs");
    shell.open();

    final Button opener = new Button(shell, SWT.PUSH);
    opener.setText("Click Me");
    opener.setBounds(20, 20, 50, 25);

    final Text text = new Text(shell, SWT.SHADOW_IN);
    text.setBounds(80, 20, 100, 25);

    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
    dialog.setText("Dialog");
    dialog.setSize(150, 100);

    final Label label = new Label(dialog, SWT.NONE);
    label.setText("OK to proceed?");
    label.setBounds(35, 5, 100, 20);

    final Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setBounds(20, 35, 40, 25);
    okButton.setText("OK");

    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setBounds(70, 35, 40, 25);
    cancelButton.setText("Cancel");

    final boolean[] response = new boolean[1];
    response[0] = true;

    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            if (event.widget == okButton) {
                response[0] = true;
            } else {
                response[0] = false;
            }
            dialog.close();
        }
    };

    okButton.addListener(SWT.Selection, listener);
    cancelButton.addListener(SWT.Selection, listener);

    Listener openerListener = new Listener() {
        public void handleEvent(Event event) {
            dialog.open();
        }
    };

    opener.addListener(SWT.Selection, openerListener);

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

    if (response[0]) {
        text.setText("You clicked OK");
    } else {
        text.setText("You clicked Cancel");
    }

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

From source file:SWTButtonExample.java

SWTButtonExample() {
    d = new Display();
    s = new Shell(d);
    s.setSize(150, 150);//w w  w . j a  v a 2  s.  c  o m

    s.setText("A Button Example");
    final Button b1 = new Button(s, SWT.PUSH);
    b1.setBounds(50, 50, 75, 40);
    b1.setText("Push Me");
    s.open();
    while (!s.isDisposed()) {
        if (!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
}

From source file:ImageButton.java

public ImageButton() {
    //shell.setLayout(new RowLayout());

    Button button = new Button(shell, SWT.PUSH);
    button.setBounds(10, 10, 200, 200);

    button.setImage(image);/*from   www  .  j  a  va  2 s.  co m*/
    button.setText("button");

    System.out.println(button.getImage());

    shell.pack();
    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();
}

From source file:SimpleCanvas.java

/**
 * Creates the main window's contents//w  ww.  ja  va2s. c om
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());

    // Create a canvas
    Canvas canvas = new Canvas(shell, SWT.NONE);

    // Create a button on the canvas
    Button button = new Button(canvas, SWT.PUSH);
    button.setBounds(10, 10, 300, 40);
    button.setText("You can place widgets on a canvas");

    // Create a paint handler for the canvas
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            // Do some drawing
            Rectangle rect = ((Canvas) e.widget).getBounds();
            e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED));
            e.gc.drawFocus(5, 5, rect.width - 10, rect.height - 10);
            e.gc.drawText("You can draw text directly on a canvas", 60, 60);
        }
    });
}

From source file:ListExample3.java

ListExample3() {
    d = new Display();
    s = new Shell(d);
    s.setSize(250, 250);//from  ww w. j  av  a 2  s  .  c  o m

    s.setText("A List Example");
    final List l = new List(s, SWT.MULTI | SWT.BORDER);
    l.setBounds(50, 50, 75, 75);
    l.add("Item One");
    l.add("Item Two");
    l.add("Item Three");
    l.add("Item Four");
    l.add("Item Five");
    final Button b1 = new Button(s, SWT.PUSH | SWT.BORDER);
    b1.setBounds(150, 150, 50, 25);
    b1.setText("Click Me");
    b1.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            String selected[] = l.getSelection();
            for (int i = 0; i < selected.length; i++) {
                System.out.println(selected[i]);
            }
        }
    });
    s.open();
    while (!s.isDisposed()) {
        if (!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
}

From source file:ArrowButtonExample.java

ArrowButtonExample() {
    d = new Display();
    s = new Shell(d);
    s.setSize(250, 250);//from w w w .  j  a va  2s .c  o m

    s.setText("A Button Example");
    final Button b1 = new Button(s, SWT.ARROW | SWT.UP);
    b1.setBounds(100, 55, 20, 15);
    final Button b2 = new Button(s, SWT.ARROW | SWT.DOWN);
    b2.setBounds(100, 70, 20, 15);
    final Text t1 = new Text(s, SWT.BORDER | SWT.SINGLE | SWT.CENTER);
    t1.setBounds(80, 55, 20, 30);
    t1.setText("1");
    t1.selectAll();
    b1.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            int n = new Integer(t1.getText()).intValue();
            n++;
            t1.setText(new Integer(n).toString());
            t1.selectAll();
        }
    });
    b2.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            int n = new Integer(t1.getText()).intValue();
            n--;
            t1.setText(new Integer(n).toString());
            t1.selectAll();
        }
    });
    s.open();
    while (!s.isDisposed()) {
        if (!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
}

From source file:MouseMoveListenerExample.java

public MouseMoveListenerExample() {
    d = new Display();
    s = new Shell(d);

    s.setSize(250, 200);//  w w w . j  a v  a2 s  .  c o m

    s.setText("A MouseListener Example");
    final Button b = new Button(s, SWT.PUSH);
    b.setText("Push Me");
    b.setBounds(20, 50, 55, 25);
    s.open();

    b.addMouseMoveListener(new MouseMoveListener() {
        public void mouseMove(MouseEvent e) {
            Random r = new Random(System.currentTimeMillis());
            Point p = s.getSize();
            int newX = r.nextInt(p.y);
            int newY = r.nextInt(p.x);
            b.setBounds(newX - 55, newY - 25, 55, 25);
        }

    });

    while (!s.isDisposed()) {
        if (!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
}