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

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

Introduction

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

Prototype

public Button(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

From source file:MainClass.java

public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);

    s.setText("A Tabbed Shell Example");
    s.setLayout(new FillLayout());

    TabFolder tf = new TabFolder(s, SWT.BORDER);

    TabItem ti1 = new TabItem(tf, SWT.BORDER);
    ti1.setText("Option Group");
    ti1.setControl(new Text(tf, SWT.MULTI));

    TabItem ti2 = new TabItem(tf, SWT.BORDER);
    ti2.setText("Grid");
    ti2.setControl(new Text(tf, SWT.MULTI));

    TabItem ti3 = new TabItem(tf, SWT.BORDER);
    ti3.setText("Text");
    Composite c1 = new Composite(tf, SWT.BORDER);
    c1.setLayout(new FillLayout());
    Text t = new Text(c1, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
    ti3.setControl(c1);/*from   w w  w . j  a v  a2  s .c  o m*/

    TabItem ti4 = new TabItem(tf, SWT.BORDER);
    ti4.setText("Settings");
    Composite c2 = new Composite(tf, SWT.BORDER);
    c2.setLayout(new RowLayout());
    Text t2 = new Text(c2, SWT.BORDER | SWT.SINGLE | SWT.WRAP | SWT.V_SCROLL);
    Button b = new Button(c2, SWT.PUSH | SWT.BORDER);
    b.setText("Save");
    ti4.setControl(c2);

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

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 109");
    shell.setLayout(new FillLayout());

    SashForm form = new SashForm(shell, SWT.HORIZONTAL);
    form.setLayout(new FillLayout());

    Composite child1 = new Composite(form, SWT.NONE);
    child1.setLayout(new FillLayout());
    new Label(child1, SWT.NONE).setText("Label in pane 1");

    Composite child2 = new Composite(form, SWT.NONE);
    child2.setLayout(new FillLayout());
    new Button(child2, SWT.PUSH).setText("Button in pane2");

    Composite child3 = new Composite(form, SWT.NONE);
    child3.setLayout(new FillLayout());
    new Label(child3, SWT.PUSH).setText("Label in pane3");

    form.setWeights(new int[] { 30, 40, 30 });
    shell.open();/*from  ww w.  j ava 2s  .c o  m*/
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ScreenShotWithGC.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {

            /* Take the screen shot */
            GC gc = new GC(display);
            final Image image = new Image(display, display.getBounds());
            gc.copyArea(image, 0, 0);/*www  .  j  a va  2 s  .  c  o  m*/
            gc.dispose();

            Shell popup = new Shell(shell, SWT.SHELL_TRIM);
            popup.setLayout(new FillLayout());
            popup.setText("Image");
            popup.setBounds(50, 50, 200, 200);
            popup.addListener(SWT.Close, new Listener() {
                public void handleEvent(Event e) {
                    image.dispose();
                }
            });

            ScrolledComposite sc = new ScrolledComposite(popup, SWT.V_SCROLL | SWT.H_SCROLL);
            Canvas canvas = new Canvas(sc, SWT.NONE);
            sc.setContent(canvas);
            canvas.setBounds(display.getBounds());
            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.open();
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 130");
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));
    final int[] nextId = new int[1];
    Button b = new Button(shell, SWT.PUSH);
    b.setText("invoke long running job");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        Runnable longJob = new Runnable() {
            boolean done = false;
            int id;

            @Override//from w w  w.j  a  va2  s . c  o m
            public void run() {
                Thread thread = new Thread(() -> {
                    id = nextId[0]++;
                    display.syncExec(() -> {
                        if (text.isDisposed())
                            return;
                        text.append("\nStart long running task " + id);
                    });
                    for (int i = 0; i < 100000; i++) {
                        if (display.isDisposed())
                            return;
                        System.out.println("do task that takes a long time in a separate thread " + id);
                    }
                    if (display.isDisposed())
                        return;
                    display.syncExec(() -> {
                        if (text.isDisposed())
                            return;
                        text.append("\nCompleted long running task " + id);
                    });
                    done = true;
                    display.wake();
                });
                thread.start();
                while (!done && !shell.isDisposed()) {
                    if (!display.readAndDispatch())
                        display.sleep();
                }
            }
        };
        BusyIndicator.showWhile(display, longJob);
    }));
    shell.setSize(250, 150);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:SyncExecExample.java

public static void main(String[] args) {

    final Display display = new Display();
    Shell shell = new Shell(display);

    final Runnable print = new Runnable() {
        public void run() {
            System.out.println("Print from thread: \t" + Thread.currentThread().getName());
        }/*from  w  ww .j  a v  a2 s .  c  o m*/
    };

    final Thread applicationThread = new Thread("applicationThread") {
        public void run() {
            System.out.println("Hello from thread: \t" + Thread.currentThread().getName());
            display.syncExec(print);
            System.out.println("Bye from thread: \t" + Thread.currentThread().getName());
        }
    };

    shell.setText("syncExec Example");
    shell.setSize(300, 100);

    Button button = new Button(shell, SWT.CENTER);
    button.setText("Click to start");
    button.setBounds(shell.getClientArea());
    button.addSelectionListener(new SelectionListener() {
        public void widgetDefaultSelected(SelectionEvent e) {
        }

        public void widgetSelected(SelectionEvent e) {
            applicationThread.start();
        }
    });

    shell.open();

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

    display.dispose();

}

From source file:ScrollWidgetViewFocusIn.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button b1 = new Button(shell, SWT.PUSH);
    b1.setText("top");
    b1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    Composite c = new Composite(sc, SWT.NONE);
    c.setLayout(new GridLayout(10, true));
    for (int i = 0; i < 300; i++) {
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button " + i);
    }//from  w  w w. j  av a 2s  .co m
    Button b2 = new Button(shell, SWT.PUSH);
    b2.setText("bottom");
    b2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

    sc.setContent(c);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    Listener listener = new Listener() {
        public void handleEvent(Event e) {
            Control child = (Control) e.widget;
            Rectangle bounds = child.getBounds();
            Rectangle area = sc.getClientArea();
            Point origin = sc.getOrigin();
            if (origin.x > bounds.x)
                origin.x = Math.max(0, bounds.x);
            if (origin.y > bounds.y)
                origin.y = Math.max(0, bounds.y);
            if (origin.x + area.width < bounds.x + bounds.width)
                origin.x = Math.max(0, bounds.x + bounds.width - area.width);
            if (origin.y + area.height < bounds.y + bounds.height)
                origin.y = Math.max(0, bounds.y + bounds.height - area.height);
            sc.setOrigin(origin);
        }
    };
    Control[] controls = c.getChildren();
    for (int i = 0; i < controls.length; i++) {
        controls[i].addListener(SWT.Activate, listener);
    }
    shell.setSize(300, 500);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ModifyDOM.java

public static void main(String[] args) {
    final String html = "<html><title>Snippet</title><body><p id='myid'>Best Friends</p><p id='myid2'>Cat and Dog</p></body></html>";
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Browser browser = new Browser(shell, SWT.BORDER);
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout(SWT.VERTICAL));
    final Text text = new Text(comp, SWT.MULTI);
    text.setText("var newNode = document.createElement('P'); \r\n"
            + "var text = document.createTextNode('At least when I am around');\r\n"
            + "newNode.appendChild(text);\r\n" + "document.getElementById('myid').appendChild(newNode);\r\n"
            + "\r\n" + "document.bgColor='yellow';");
    final Button button = new Button(comp, SWT.PUSH);
    button.setText("Execute Script");
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            boolean result = browser.execute(text.getText());
            if (!result) {
                /*/*from  ww w.j av a 2  s.  c o m*/
                 * Script may fail or may not be supported on certain
                 * platforms.
                 */
                System.out.println("Script was not executed.");
            }
        }
    });
    browser.setText(html);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ScrollBarSelectionListener.java

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

    final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button button1 = new Button(sc1, SWT.PUSH);
    button1.setText("Button 1");
    button1.setSize(400, 300);//from  w w  w  . j  a  v a  2s .c o  m
    sc1.setContent(button1);

    final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button button2 = new Button(sc2, SWT.PUSH);
    button2.setText("Button 2");
    button2.setSize(300, 400);
    sc2.setContent(button2);

    final ScrollBar vBar1 = sc1.getVerticalBar();
    final ScrollBar vBar2 = sc2.getVerticalBar();
    final ScrollBar hBar1 = sc1.getHorizontalBar();
    final ScrollBar hBar2 = sc2.getHorizontalBar();
    SelectionListener listener1 = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            int x = hBar1.getSelection() * (hBar2.getMaximum() - hBar2.getThumb())
                    / Math.max(1, hBar1.getMaximum() - hBar1.getThumb());
            int y = vBar1.getSelection() * (vBar2.getMaximum() - vBar2.getThumb())
                    / Math.max(1, vBar1.getMaximum() - vBar1.getThumb());
            sc2.setOrigin(x, y);
        }
    };
    SelectionListener listener2 = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            int x = hBar2.getSelection() * (hBar1.getMaximum() - hBar1.getThumb())
                    / Math.max(1, hBar2.getMaximum() - hBar2.getThumb());
            int y = vBar2.getSelection() * (vBar1.getMaximum() - vBar1.getThumb())
                    / Math.max(1, vBar2.getMaximum() - vBar2.getThumb());
            sc1.setOrigin(x, y);
        }
    };
    vBar1.addSelectionListener(listener1);
    hBar1.addSelectionListener(listener1);
    vBar2.addSelectionListener(listener2);
    hBar2.addSelectionListener(listener2);

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

From source file:ScrollBarAuto.java

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

    // this button has a minimum size of 400 x 400. If the window is resized to be big
    // enough to show more than 400 x 400, the button will grow in size. If the window
    // is made too small to show 400 x 400, scrollbars will appear.
    ScrolledComposite c2 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button b2 = new Button(c2, SWT.PUSH);
    b2.setText("expanding button");
    c2.setContent(b2);//from w  w  w.j a  v a 2s  .  c  om
    c2.setExpandHorizontal(true);
    c2.setExpandVertical(true);
    c2.setMinWidth(400);
    c2.setMinHeight(400);

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

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

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

    Label label = new Label(shell, SWT.WRAP | SWT.BORDER);
    GridData labelData = new GridData();
    labelData.widthHint = 10; /* default width */
    labelData.horizontalAlignment = SWT.FILL; /* grow to fill available width */
    label.setLayoutData(labelData);//from  ww w.  j av a 2  s .  co m
    label.setText(
            "Snippets are minimal stand-alone programs that demonstrate specific techniques or functionality.");
    new Button(shell, SWT.PUSH).setText("This button determines the Shell's width");

    /* do an initial pack() so that the Shell determines its required width */
    shell.pack();

    /* update the Label's width hint to match what the layout allocated for it */
    labelData.widthHint = label.getBounds().width;

    /*
     * do a second pack() so that the Label will compute its required height
     * based on its correct width instead of its previously-set default width
     */
    shell.pack();

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