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:org.eclipse.swt.snippets.Snippet164.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 164");
    shell.setLayout(new GridLayout());
    Image image = new Image(display, Snippet164.class.getResourceAsStream("eclipse.png"));

    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("&Typical button");

    Button button2 = new Button(shell, SWT.PUSH);
    button2.setImage(image);/*from  ww w .  j a  v  a  2s  .  c  o  m*/
    button2.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getName(AccessibleEvent e) {
            e.result = "Eclipse logo";
        }
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 176");
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;//from   ww w .  ja  v  a2s  .com
    layout.fill = false;
    layout.justify = true;
    shell.setLayout(layout);

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);

    b.setText("Button 2");

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");

    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 177");
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.wrap = true;//from w  w w . j  a  va  2  s.c  om
    layout.fill = true;
    layout.justify = false;
    shell.setLayout(layout);

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);

    b.setText("Button 2");

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");

    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);

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

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

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

    Button open = new Button(shell, SWT.PUSH);
    open.setText("Open Dialog");
    open.addSelectionListener(widgetSelectedAdapter(e -> {
        final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
        dialog.setLayout(new GridLayout(3, false));

        final DateTime calendar = new DateTime(dialog, SWT.CALENDAR | SWT.BORDER);
        final DateTime date = new DateTime(dialog, SWT.DATE | SWT.SHORT);
        final DateTime time = new DateTime(dialog, SWT.TIME | SWT.SHORT);

        new Label(dialog, SWT.NONE);
        new Label(dialog, SWT.NONE);
        Button ok = new Button(dialog, SWT.PUSH);
        ok.setText("OK");
        ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        ok.addSelectionListener(widgetSelectedAdapter(event -> {
            System.out.println("Calendar date selected (MM/DD/YYYY) = " + (calendar.getMonth() + 1) + "/"
                    + calendar.getDay() + "/" + calendar.getYear());
            System.out.println("Date selected (MM/YYYY) = " + (date.getMonth() + 1) + "/" + date.getYear());
            System.out.println("Time selected (HH:MM) = " + time.getHours() + ":"
                    + (time.getMinutes() < 10 ? "0" : "") + time.getMinutes());
            dialog.close();// w ww  .  j  av a  2 s. c  o  m
        }));
        dialog.setDefaultButton(ok);
        dialog.pack();
        dialog.open();
    }));
    shell.pack();
    shell.open();

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 71");
    shell.pack();//from   w w w .j a  v a 2 s  . c  o  m
    shell.open();
    Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
    Label label = new Label(dialog, SWT.NONE);
    label.setText("Exit the application?");
    Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setText("&OK");
    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setText("&Cancel");

    FormLayout form = new FormLayout();
    form.marginWidth = form.marginHeight = 8;
    dialog.setLayout(form);
    FormData okData = new FormData();
    okData.top = new FormAttachment(label, 8);
    okButton.setLayoutData(okData);
    FormData cancelData = new FormData();
    cancelData.left = new FormAttachment(okButton, 8);
    cancelData.top = new FormAttachment(okButton, 0, SWT.TOP);
    cancelButton.setLayoutData(cancelData);

    dialog.setDefaultButton(okButton);
    dialog.pack();
    dialog.open();

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

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

public static void main(String[] args) {
    final 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 68");
    shell.setLayout(new RowLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Stop Timer");
    final Label label = new Label(shell, SWT.BORDER);
    label.setBackground(red);/*from  w  w w.ja  v  a  2s  .  c o m*/
    final int time = 500;
    final Runnable timer = new Runnable() {
        @Override
        public void run() {
            if (label.isDisposed())
                return;
            Color color = label.getBackground().equals(red) ? blue : red;
            label.setBackground(color);
            display.timerExec(time, this);
        }
    };
    display.timerExec(time, timer);
    button.addListener(SWT.Selection, event -> display.timerExec(-1, timer));
    button.pack();
    label.setLayoutData(new RowData(button.getSize()));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 63");
    shell.pack();/*w  w w . j  a  v  a  2s.  c  o m*/
    shell.open();
    final boolean[] result = new boolean[1];
    final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
    dialog.setLayout(new RowLayout());
    final Button ok = new Button(dialog, SWT.PUSH);
    ok.setText("OK");
    Button cancel = new Button(dialog, SWT.PUSH);
    cancel.setText("Cancel");
    Listener listener = event -> {
        result[0] = event.widget == ok;
        dialog.close();
    };
    ok.addListener(SWT.Selection, listener);
    cancel.addListener(SWT.Selection, listener);
    dialog.pack();
    dialog.open();
    System.out.println("Prompt ...");
    while (!dialog.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    System.out.println("Result: " + result[0]);
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 46");
    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setEnabled(false);//  www.  j  a  v  a2s.  c  o m
    composite.setLayout(new FillLayout());
    Button button = new Button(composite, SWT.PUSH);
    button.setText("Button");
    composite.pack();
    composite.setLocation(10, 10);
    final Point[] offset = new Point[1];
    Listener listener = event -> {
        switch (event.type) {
        case SWT.MouseDown:
            Rectangle rect = composite.getBounds();
            if (rect.contains(event.x, event.y)) {
                Point pt1 = composite.toDisplay(0, 0);
                Point pt2 = shell.toDisplay(event.x, event.y);
                offset[0] = new Point(pt2.x - pt1.x, pt2.y - pt1.y);
            }
            break;
        case SWT.MouseMove:
            if (offset[0] != null) {
                Point pt = offset[0];
                composite.setLocation(event.x - pt.x, event.y - pt.y);
            }
            break;
        case SWT.MouseUp:
            offset[0] = null;
            break;
        }
    };
    shell.addListener(SWT.MouseDown, listener);
    shell.addListener(SWT.MouseUp, listener);
    shell.addListener(SWT.MouseMove, listener);
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:GridLayoutWidgetExclude.java

public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, false));

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 0");

    final Button bHidden = new Button(shell, SWT.PUSH);
    bHidden.setText("Button 1");
    GridData data = new GridData();
    data.exclude = true;/*from  w ww  . j a  v  a2 s .  com*/
    data.horizontalSpan = 2;
    data.horizontalAlignment = SWT.FILL;
    bHidden.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 2");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 4");

    b = new Button(shell, SWT.CHECK);
    b.setText("hide");
    b.setSelection(true);
    b.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            Button b = (Button) e.widget;
            GridData data = (GridData) bHidden.getLayoutData();
            data.exclude = b.getSelection();
            bHidden.setVisible(!data.exclude);
            shell.layout(false);
        }
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet95.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Widget");
    shell.setBounds(10, 10, 200, 200);//w ww .  j a va2  s  .  c  o  m

    final Table table = new Table(shell, SWT.MULTI);
    table.setLinesVisible(true);
    table.setBounds(10, 10, 100, 100);
    for (int i = 0; i < 9; i++) {
        new TableItem(table, SWT.NONE).setText("item" + i);
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.setBounds(10, 140, 50, 20);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            Point tableSize = table.getSize();
            GC gc = new GC(table);
            final Image image = new Image(display, tableSize.x, tableSize.y);
            gc.copyArea(image, 0, 0);
            gc.dispose();

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

            Canvas canvas = new Canvas(popup, SWT.NONE);
            canvas.setBounds(10, 10, 150, 150);
            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.open();
        }
    });

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