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:FocusListenerExample.java

/**
 * The application entry point/*w  w w.ja  v  a2s  .  c  o  m*/
 * 
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // Create the shell
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, true));
    shell.setText("One Potato, Two Potato");

    // Create the focus listener
    FocusListener listener = new FocusListener() {
        public void focusGained(FocusEvent event) {
            Button button = (Button) event.getSource();
            button.setText("I'm It!");
        }

        public void focusLost(FocusEvent event) {
            Button button = (Button) event.getSource();
            button.setText("Pick Me!");
        }
    };

    // Create the buttons and add the listener to each one
    for (int i = 0; i < 6; i++) {
        Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        button.setText("Pick Me!");
        button.addFocusListener(listener);
    }

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

From source file:Snippet169.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Listener listener = new Listener() {
        public void handleEvent(Event e) {
            Control[] children = shell.getChildren();
            for (int i = 0; i < children.length; i++) {
                Control child = children[i];
                if (e.widget != child && child instanceof Button && (child.getStyle() & SWT.TOGGLE) != 0) {
                    ((Button) child).setSelection(false);
                }//from ww  w .j  a va 2  s .c  o  m
            }
            ((Button) e.widget).setSelection(true);
        }
    };
    for (int i = 0; i < 20; i++) {
        Button button = new Button(shell, SWT.TOGGLE);
        button.setText("B" + i);
        button.addListener(SWT.Selection, listener);
        if (i == 0)
            button.setSelection(true);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

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

    Image i = new Image(display, Snippet364.class.getResourceAsStream("eclipse.png"));
    Button b = new Button(shell, SWT.PUSH | SWT.LEFT_TO_RIGHT);
    b.setText("Button LEFT_TO_RIGHT...");
    b.setImage(i);//from  ww  w. j av a 2 s . c  o m

    Button b2 = new Button(shell, SWT.PUSH | SWT.RIGHT_TO_LEFT);
    b2.setText("Button RIGHT_TO_LEFT...");
    b2.setImage(i);

    new Label(shell, SWT.NONE).setText("with FLIP_TEXT_DIRECTION:");

    Button b3 = new Button(shell, SWT.PUSH | SWT.LEFT_TO_RIGHT | SWT.FLIP_TEXT_DIRECTION);
    b3.setText("Button LEFT_TO_RIGHT...");
    b3.setImage(i);

    Button b4 = new Button(shell, SWT.PUSH | SWT.RIGHT_TO_LEFT | SWT.FLIP_TEXT_DIRECTION);
    b4.setText("Button RIGHT_TO_LEFT...");
    b4.setImage(i);

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

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

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

    final Table table = new Table(shell, SWT.MULTI);
    table.setLinesVisible(true);/*w  w w  .j a v  a 2  s .c om*/
    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.pack();
    button.setLocation(10, 140);
    button.addListener(SWT.Selection, 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.addListener(SWT.Close, e -> image.dispose());

        Canvas canvas = new Canvas(popup, SWT.NONE);
        canvas.setBounds(10, 10, tableSize.x + 10, tableSize.y + 10);
        canvas.addPaintListener(e -> e.gc.drawImage(image, 0, 0));
        popup.pack();
        popup.open();
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 6");
    shell.setLayout(new GridLayout());
    final Composite c = new Composite(shell, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;/*from w w  w . java2s .c  o m*/
    c.setLayout(layout);
    for (int i = 0; i < 10; i++) {
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button " + i);
    }

    Button b = new Button(shell, SWT.PUSH);
    b.setText("add a new button at row 2 column 1");
    final int[] index = new int[1];
    b.addListener(SWT.Selection, e -> {
        Button s = new Button(c, SWT.PUSH);
        s.setText("Special " + index[0]);
        index[0]++;
        Control[] children = c.getChildren();
        s.moveAbove(children[3]);
        shell.layout(new Control[] { s });
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Clipboard cb = new Clipboard(display);
    final Shell shell = new Shell(display);
    shell.setText("Snippet 94");
    shell.setLayout(new FormLayout());
    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);

    Button copy = new Button(shell, SWT.PUSH);
    copy.setText("Copy");
    copy.addListener(SWT.Selection, e -> {
        String textData = text.getSelectionText();
        if (textData.length() > 0) {
            TextTransfer textTransfer = TextTransfer.getInstance();
            cb.setContents(new Object[] { textData }, new Transfer[] { textTransfer });
        }/*from w  ww .jav  a  2s  .c  o m*/
    });

    Button paste = new Button(shell, SWT.PUSH);
    paste.setText("Paste");
    paste.addListener(SWT.Selection, e -> {
        TextTransfer transfer = TextTransfer.getInstance();
        String data = (String) cb.getContents(transfer);
        if (data != null) {
            text.insert(data);
        }
    });

    FormData data = new FormData();
    data.left = new FormAttachment(paste, 0, SWT.LEFT);
    data.right = new FormAttachment(100, -5);
    data.top = new FormAttachment(0, 5);
    copy.setLayoutData(data);

    data = new FormData();
    data.right = new FormAttachment(100, -5);
    data.top = new FormAttachment(copy, 5);
    paste.setLayoutData(data);

    data = new FormData();
    data.left = new FormAttachment(0, 5);
    data.top = new FormAttachment(0, 5);
    data.right = new FormAttachment(paste, -5);
    data.bottom = new FormAttachment(100, -5);
    text.setLayoutData(data);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 108");
    Label label = new Label(shell, SWT.NONE);
    label.setText("Enter your name:");
    Text text = new Text(shell, SWT.BORDER);
    text.setLayoutData(new RowData(100, SWT.DEFAULT));
    Button ok = new Button(shell, SWT.PUSH);
    ok.setText("OK");
    ok.addSelectionListener(widgetSelectedAdapter(e -> System.out.println("OK")));
    Button cancel = new Button(shell, SWT.PUSH);
    cancel.setText("Cancel");
    cancel.addSelectionListener(widgetSelectedAdapter(e -> System.out.println("Cancel")));
    shell.setDefaultButton(cancel);/*from  w ww .j a  v  a  2 s  .c  om*/
    shell.setLayout(new RowLayout());
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Shell");
    FillLayout fillLayout = new FillLayout();
    fillLayout.marginWidth = 10;/* w  ww.j a  v  a 2s. c o m*/
    fillLayout.marginHeight = 10;
    shell.setLayout(fillLayout);

    Button open = new Button(shell, SWT.PUSH);
    open.setText("Prompt for a String");
    open.addSelectionListener(widgetSelectedAdapter(e -> {
        final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        dialog.setText("Dialog Shell");
        FormLayout formLayout = new FormLayout();
        formLayout.marginWidth = 10;
        formLayout.marginHeight = 10;
        formLayout.spacing = 10;
        dialog.setLayout(formLayout);

        Label label = new Label(dialog, SWT.NONE);
        label.setText("Type a String:");
        FormData data = new FormData();
        label.setLayoutData(data);

        Button cancel = new Button(dialog, SWT.PUSH);
        cancel.setText("Cancel");
        data = new FormData();
        data.width = 60;
        data.right = new FormAttachment(100, 0);
        data.bottom = new FormAttachment(100, 0);
        cancel.setLayoutData(data);
        cancel.addSelectionListener(widgetSelectedAdapter(event -> {
            System.out.println("User cancelled dialog");
            dialog.close();
        }));

        final Text text = new Text(dialog, SWT.BORDER);
        data = new FormData();
        data.width = 200;
        data.left = new FormAttachment(label, 0, SWT.DEFAULT);
        data.right = new FormAttachment(100, 0);
        data.top = new FormAttachment(label, 0, SWT.CENTER);
        data.bottom = new FormAttachment(cancel, 0, SWT.DEFAULT);
        text.setLayoutData(data);

        Button ok = new Button(dialog, SWT.PUSH);
        ok.setText("OK");
        data = new FormData();
        data.width = 60;
        data.right = new FormAttachment(cancel, 0, SWT.DEFAULT);
        data.bottom = new FormAttachment(100, 0);
        ok.setLayoutData(data);
        ok.addSelectionListener(widgetSelectedAdapter(event -> {
            System.out.println("User typed: " + text.getText());
            dialog.close();
        }));

        dialog.setDefaultButton(ok);
        dialog.pack();
        dialog.open();
    }));
    shell.pack();
    shell.open();

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

From source file:RowLayoutAlignWidgets.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;//www .  j  a va2s .  c  o  m
    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:RowLayoutAlignColumn.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;//from  w  w  w. j a  v a2 s . c o  m
    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();
}