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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setEnabled(false);/*from   w w  w .  ja  v  a2  s.  com*/
    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 = new Listener() {
        public void handleEvent(Event 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:FormLayoutDialogDemo.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("Some text for your dialog.");
    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  w  w  . j  ava  2s  . c  om
    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();

}

From source file:Snippet6.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Composite c = new Composite(shell, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;//from  w w  w. ja  v  a  2  s  . 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, new Listener() {
        public void handleEvent(Event 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:SWTButtonAction.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(200, 200);/*from   w  w  w  . ja  v  a2s .  c  om*/
    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:Snippet75.java

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

    Composite c1 = new Composite(shell, SWT.BORDER);
    c1.setLayout(new RowLayout());
    Button b1 = new Button(c1, SWT.PUSH);
    b1.setText("B1");
    Button[] radios = new Button[3];
    for (int i = 0; i < radios.length; i++) {
        radios[i] = new Button(c1, SWT.RADIO);
        radios[i].setText("R" + (i == 1 ? "&" : "") + i);
    }/*w ww .  j  a  va 2  s  . c om*/
    Button b2 = new Button(c1, SWT.PUSH);
    b2.setText("B2");
    List l1 = new List(c1, SWT.SINGLE | SWT.BORDER);
    l1.setItems(new String[] { "L1" });
    Button b3 = new Button(c1, SWT.PUSH);
    b3.setText("B&3");
    Button b3_1 = new Button(c1, SWT.PUSH);
    b3_1.setText("B3_1");

    Composite c2 = new Composite(shell, SWT.BORDER);
    c2.setLayout(new RowLayout());
    Button b4 = new Button(c2, SWT.PUSH);
    b4.setText("B&4");
    Button b5 = new Button(c2, SWT.PUSH);
    b5.setText("B&5");

    List l2 = new List(shell, SWT.SINGLE | SWT.BORDER);
    l2.setItems(new String[] { "L2" });

    ToolBar tb1 = new ToolBar(shell, SWT.FLAT | SWT.BORDER);
    ToolItem i1 = new ToolItem(tb1, SWT.RADIO);
    i1.setText("I1");
    ToolItem i2 = new ToolItem(tb1, SWT.RADIO);
    i2.setText("I&2");
    Combo combo1 = new Combo(tb1, SWT.READ_ONLY | SWT.BORDER);
    combo1.setItems(new String[] { "C1" });
    combo1.setText("C1");
    combo1.pack();
    ToolItem i3 = new ToolItem(tb1, SWT.SEPARATOR);
    i3.setWidth(combo1.getSize().x);
    i3.setControl(combo1);
    i3.setText("I3");
    ToolItem i4 = new ToolItem(tb1, SWT.PUSH);
    i4.setText("I4");
    ToolItem i5 = new ToolItem(tb1, SWT.CHECK);
    i5.setText("I5");

    Button b6 = new Button(shell, SWT.PUSH);
    b6.setText("B&6");

    Composite c4 = new Composite(shell, SWT.BORDER);
    c4.setSize(32, 32);
    Composite c5 = new Composite(c4, SWT.BORDER);
    c5.setSize(20, 20);

    Control[] list1 = new Control[] { b2, b1, b3_1, b3 };
    c1.setTabList(list1);
    Control[] list2 = new Control[] { c1, b6, tb1, c4, c2, l2 };
    shell.setTabList(list2);

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

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

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

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

    Button b1 = new Button(shell, SWT.CHECK);
    b1.setText("State 1");
    b1.setSelection(true);//from  ww  w .  ja  v  a2  s  .c  o  m

    Button b2 = new Button(shell, SWT.CHECK);
    b2.setText("State 2");
    b2.setSelection(false);

    Button b3 = new Button(shell, SWT.CHECK);
    b3.setText("State 3");
    b3.setSelection(true);
    b3.setGrayed(true);

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

From source file:ControlEditorWithDialog.java

public static void main(String[] args) {

    final Shell shell = new Shell(display);
    shell.setText("Control Editor Two");

    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setBackground(color);/*from ww w  . ja  v  a2  s  .c o m*/
    composite.setBounds(0, 0, 300, 100);

    ControlEditor editor = new ControlEditor(composite);

    // Create the control associated with the editor
    Button button = new Button(composite, SWT.PUSH);
    button.setText("Change Color...");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            ColorDialog dialog = new ColorDialog(shell);
            if (color != null)
                dialog.setRGB(color.getRGB());
            RGB rgb = dialog.open();
            if (rgb != null) {
                if (color != null)
                    color.dispose();
                color = new Color(shell.getDisplay(), rgb);
                composite.setBackground(color);
            }
        }
    });
    // Place the editor along the bottom of the parent composite
    editor.grabHorizontal = true;
    editor.verticalAlignment = SWT.BOTTOM;
    Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    editor.minimumHeight = size.y;
    editor.setEditor(button);

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

From source file:ExpandBarControlsAdding.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("ExpandBar Example");
    ExpandBar bar = new ExpandBar(shell, SWT.V_SCROLL);
    Image image = new Image(display, "yourFile.gif");

    // First item
    Composite composite = new Composite(bar, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
    layout.verticalSpacing = 10;//from  w w  w .j ava2  s. c o  m
    composite.setLayout(layout);
    Button button = new Button(composite, SWT.PUSH);
    button.setText("SWT.PUSH");
    button = new Button(composite, SWT.RADIO);
    button.setText("SWT.RADIO");
    button = new Button(composite, SWT.CHECK);
    button.setText("SWT.CHECK");
    button = new Button(composite, SWT.TOGGLE);
    button.setText("SWT.TOGGLE");
    ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
    item0.setText("What is your favorite button");
    item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    item0.setControl(composite);
    item0.setImage(image);

    item0.setExpanded(true);

    bar.setSpacing(8);
    shell.setSize(400, 350);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    image.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 127");
    shell.setLayout(new RowLayout());
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Can't Traverse");
    button1.addTraverseListener(e -> {
        switch (e.detail) {
        case SWT.TRAVERSE_TAB_NEXT:
        case SWT.TRAVERSE_TAB_PREVIOUS: {
            e.doit = false;/* ww  w. j av a 2s . co  m*/
        }
        }
    });
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Can Traverse");
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet251.java

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

    Button open = new Button(shell, SWT.PUSH);
    open.setText("Open Dialog");
    open.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent 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(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    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());
                    dialog.close();//from  w ww  .j  a  v  a 2 s  . c om
                }
            });
            dialog.setDefaultButton(ok);
            dialog.pack();
            dialog.open();
        }
    });
    shell.pack();
    shell.open();

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