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

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

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Click me");
    button.addSelectionListener(widgetSelectedAdapter(e -> {
        Shell shell2 = new Shell(SWT.TOOL | SWT.RESIZE | SWT.CLOSE | SWT.MAX);
        shell2.setLayout(new GridLayout(1, false));
        shell2.setText("Palette");
        Label l = new Label(shell2, SWT.LEFT);
        l.setText("This is a SWT.TOOL Shell");
        Point origin = shell.getLocation();
        origin.x += 100;/*from  w ww .  j a  v  a2s. c  o  m*/
        origin.y += 100;
        shell2.pack();
        shell2.open();
    }));

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

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

public static void main(String args[]) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 98");
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Push");
    pageComposite = new Composite(shell, SWT.NONE);
    pageComposite.setLayout(new GridLayout());
    pageComposite.setLayoutData(new GridData());

    button.addListener(SWT.Selection, event -> {
        if ((pageComposite != null) && (!pageComposite.isDisposed())) {
            pageComposite.dispose();//from  ww  w. ja  v a  2s .  c  o  m
        }
        pageComposite = new Composite(shell, SWT.NONE);
        pageComposite.setLayout(new GridLayout());
        pageComposite.setLayoutData(new GridData());
        if (pageNum++ % 2 == 0) {
            Table table = new Table(pageComposite, SWT.BORDER);
            table.setLayoutData(new GridData());
            for (int i = 0; i < 5; i++) {
                new TableItem(table, SWT.NONE).setText("table item " + i);
            }
        } else {
            new Button(pageComposite, SWT.RADIO).setText("radio");
        }
        shell.layout(true);
    });

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

From source file:MouseEventPost.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);//w  w  w  .j a  va2s. c  om
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
        }
    });
    final Point pt = display.map(shell, null, 50, 50);
    new Thread() {
        Event event;

        public void run() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event = new Event();
            event.type = SWT.MouseMove;
            event.x = pt.x;
            event.y = pt.y;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell();
    shell.setText("Snippet 345");
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    String string = "The quick brown fox jumps over the lazy dog";
    Button button;/* ww  w. ja  v  a  2 s  .co  m*/
    button = new Button(shell, SWT.PUSH | SWT.WRAP);
    button.setText(string);
    button = new Button(shell, SWT.RADIO | SWT.WRAP);
    button.setText(string);
    button = new Button(shell, SWT.TOGGLE | SWT.WRAP);
    button.setText(string);
    button = new Button(shell, SWT.CHECK | SWT.WRAP);
    button.setText(string);
    shell.setSize(shell.computeSize(200, SWT.DEFAULT));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:org.eclipse.swt.examples.accessibility.AccessibleNameExample.java

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

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button"); // the first button's accessible name is "Button"

    Image image = new Image(display, AccessibleNameExample.class.getResourceAsStream("run.gif"));
    button = new Button(shell, SWT.PUSH);
    button.setImage(image);//  w  w  w . j  av a 2  s  .c o  m
    button.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getName(AccessibleEvent e) {
            e.result = "Running man"; // the second button's accessible name is "Running man"
        }
    });

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

From source file:Snippet103.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 200, 240);// w w  w  .  j a  v  a 2s.  co m
    Table table = new Table(shell, SWT.NONE);
    table.setBounds(10, 10, 160, 160);

    final TableItem[] items = new TableItem[4];
    for (int i = 0; i < 4; i++) {
        new TableColumn(table, SWT.NONE).setWidth(40);
    }
    for (int i = 0; i < 4; i++) {
        items[i] = new TableItem(table, SWT.NONE);
        populateItem(items[i]);
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setBounds(10, 180, 50, 30);
    button.setText("Change");
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            for (int i = 0; i < 4; i++) {
                populateItem(items[i]);
            }
        }
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 172");
    GridLayout layout = new GridLayout(4, false);
    shell.setLayout(layout);/*from  w w w .  j  av a2 s  . c o  m*/

    Button b = new Button(shell, SWT.PUSH);
    b.setText("LEFT, TOP");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("LEFT, CENTER");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("LEFT, BOTTOM");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("LEFT, FILL");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("CENTER, TOP");
    b.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("CENTER, CENTER");
    b.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("CENTER, BOTTOM");
    b.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("CENTER, FILL");
    b.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("RIGHT, TOP");
    b.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("RIGHT, CENTER");
    b.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("RIGHT, BOTTOM");
    b.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("RIGHT, FILL");
    b.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("FILL, TOP");
    b.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("FILL, CENTER");
    b.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("FILL, BOTTOM");
    b.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("FILL, FILL");
    b.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

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

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

public static void main(String[] args) {
    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 21");
    Button b = new Button(shell, SWT.PUSH);
    Rectangle clientArea = shell.getClientArea();
    b.setBounds(clientArea.x + 10, clientArea.y + 10, 100, 32);
    b.setText("Button");
    shell.setDefaultButton(b);/* w  ww  .  j a  v  a  2s.co m*/
    final Canvas c = new Canvas(shell, SWT.BORDER);
    c.setBounds(10, 50, 100, 32);
    c.addListener(SWT.Traverse, e -> {
        switch (e.detail) {
        /* Do tab group traversal */
        case SWT.TRAVERSE_ESCAPE:
        case SWT.TRAVERSE_RETURN:
        case SWT.TRAVERSE_TAB_NEXT:
        case SWT.TRAVERSE_TAB_PREVIOUS:
        case SWT.TRAVERSE_PAGE_NEXT:
        case SWT.TRAVERSE_PAGE_PREVIOUS:
            e.doit = true;
            break;
        }
    });
    c.addListener(SWT.FocusIn, e -> c.setBackground(red));
    c.addListener(SWT.FocusOut, e -> c.setBackground(blue));
    c.addListener(SWT.KeyDown, e -> System.out.println("KEY"));
    Text t = new Text(shell, SWT.SINGLE | SWT.BORDER);
    t.setBounds(10, 85, 100, 32);

    Text r = new Text(shell, SWT.MULTI | SWT.BORDER);
    r.setBounds(10, 120, 100, 32);

    c.setFocus();
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

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

    final Button button = new Button(shell, SWT.PUSH);
    button.setText(">>");
    FormData data = new FormData();
    data.top = new FormAttachment(0, 4);
    data.right = new FormAttachment(100, -4);
    button.setLayoutData(data);//  w  w w.  jav a2  s .  co  m

    final Composite composite = new Composite(shell, SWT.BORDER);
    final FormData down = new FormData();
    down.top = new FormAttachment(button, 4, SWT.BOTTOM);
    down.bottom = new FormAttachment(100, -4);
    down.left = new FormAttachment(0, 4);
    down.right = new FormAttachment(100, -4);
    final FormData up = new FormData();
    up.top = new FormAttachment(0);
    up.bottom = new FormAttachment(0);
    up.left = new FormAttachment(0);
    up.right = new FormAttachment(0);
    composite.setLayoutData(up);

    button.addSelectionListener(widgetSelectedAdapter(e -> {
        if (composite.getLayoutData() == up) {
            composite.setLayoutData(down);
            button.setText("<<");
        } else {
            composite.setLayoutData(up);
            button.setText(">>");
        }
        shell.pack();
    }));

    shell.pack();
    shell.open();

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

From source file:Snippet65.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.WRAP);
    label.setText("This is a long text string that will wrap when the dialog is resized.");
    List list = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    list.setItems(new String[] { "Item 1", "Item2" });
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Ok");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Cancel");

    final int insetX = 4, insetY = 4;
    FormLayout formLayout = new FormLayout();
    formLayout.marginWidth = insetX;/*  w  ww .  ja va  2  s  .  co  m*/
    formLayout.marginHeight = insetY;
    shell.setLayout(formLayout);

    Point size = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    final FormData labelData = new FormData(size.x, SWT.DEFAULT);
    labelData.left = new FormAttachment(0, 0);
    labelData.right = new FormAttachment(100, 0);
    label.setLayoutData(labelData);
    shell.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event e) {
            Rectangle rect = shell.getClientArea();
            labelData.width = rect.width - insetX * 2;
            shell.layout();
        }
    });

    FormData button2Data = new FormData();
    button2Data.right = new FormAttachment(100, -insetX);
    button2Data.bottom = new FormAttachment(100, 0);
    button2.setLayoutData(button2Data);

    FormData button1Data = new FormData();
    button1Data.right = new FormAttachment(button2, -insetX);
    button1Data.bottom = new FormAttachment(100, 0);
    button1.setLayoutData(button1Data);

    FormData listData = new FormData();
    listData.left = new FormAttachment(0, 0);
    listData.right = new FormAttachment(100, 0);
    listData.top = new FormAttachment(label, insetY);
    listData.bottom = new FormAttachment(button2, -insetY);
    list.setLayoutData(listData);

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