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

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

    FontRegistry fontRegistry = new FontRegistry(display);

    fontRegistry.put("button-text", new FontData[] { new FontData("Arial", 9, SWT.BOLD) });
    fontRegistry.put("code", new FontData[] { new FontData("Courier New", 10, SWT.NORMAL) });

    Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP);
    text.setFont(fontRegistry.get("code"));
    text.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
    text.setText("");
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 2;//  ww  w. ja v a 2  s.co m
    text.setLayoutData(gd);
    Button executeButton = new Button(shell, SWT.PUSH);
    executeButton.setText("Execute");
    executeButton.setFont(fontRegistry.get("button-text"));

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 107");
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Button 1");
    final Sash sash = new Sash(shell, SWT.VERTICAL);
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Button 2");

    final FormLayout form = new FormLayout();
    shell.setLayout(form);/*w w  w . j a  va 2s .co  m*/

    FormData button1Data = new FormData();
    button1Data.left = new FormAttachment(0, 0);
    button1Data.right = new FormAttachment(sash, 0);
    button1Data.top = new FormAttachment(0, 0);
    button1Data.bottom = new FormAttachment(100, 0);
    button1.setLayoutData(button1Data);

    final int limit = 20, percent = 50;
    final FormData sashData = new FormData();
    sashData.left = new FormAttachment(percent, 0);
    sashData.top = new FormAttachment(0, 0);
    sashData.bottom = new FormAttachment(100, 0);
    sash.setLayoutData(sashData);
    sash.addListener(SWT.Selection, e -> {
        Rectangle sashRect = sash.getBounds();
        Rectangle shellRect = shell.getClientArea();
        int right = shellRect.width - sashRect.width - limit;
        e.x = Math.max(Math.min(e.x, right), limit);
        if (e.x != sashRect.x) {
            sashData.left = new FormAttachment(0, e.x);
            shell.layout();
        }
    });

    FormData button2Data = new FormData();
    button2Data.left = new FormAttachment(sash, 0);
    button2Data.right = new FormAttachment(100, 0);
    button2Data.top = new FormAttachment(0, 0);
    button2Data.bottom = new FormAttachment(100, 0);
    button2.setLayoutData(button2Data);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 315");
    shell.setLayout(new GridLayout());
    final Button button = new Button(shell, SWT.CHECK);
    button.setLayoutData(//from  ww  w.  jav  a2 s. co  m
            new GridData(GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_CENTER));
    button.setText("Tri-state");
    /* Make the button toggle between three states */
    button.addListener(SWT.Selection, e -> {
        if (button.getSelection()) {
            if (!button.getGrayed()) {
                button.setGrayed(true);
            }
        } else {
            if (button.getGrayed()) {
                button.setGrayed(false);
                button.setSelection(true);
            }
        }
    });
    /* Read the tri-state button (application code) */
    button.addListener(SWT.Selection, e -> {
        if (button.getGrayed()) {
            System.out.println("Grayed");
        } else {
            if (button.getSelection()) {
                System.out.println("Selected");
            } else {
                System.out.println("Not selected");
            }
        }
    });
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 224");
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    for (int i = 0; i < 8; i++) {
        Button button = new Button(shell, SWT.RADIO);
        button.setText("B" + i);
        if (i == 0)
            button.setSelection(true);//from   w  w  w . java2 s  .  c o m
    }
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Set Selection to B4");
    button.addListener(SWT.Selection, event -> {
        Control[] children = shell.getChildren();
        Button newButton = (Button) children[4];
        for (int i = 0; i < children.length; i++) {
            Control child = children[i];
            if (child instanceof Button && (child.getStyle() & SWT.RADIO) != 0) {
                ((Button) child).setSelection(false);
            }
        }
        newButton.setSelection(true);
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 4");
    Button b = new Button(shell, SWT.PUSH);
    b.setText("Open Dialog ...");
    b.pack();/*from w w  w .  ja v a  2  s . c  o m*/
    Rectangle clientArea = shell.getClientArea();
    b.setLocation(clientArea.x + 10, clientArea.y + 10);
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
        dialog.addListener(SWT.Traverse, t -> {
            if (t.detail == SWT.TRAVERSE_ESCAPE) {
                t.doit = false;
            }
        });
        dialog.open();
    }));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:MainClass.java

public static void main(String[] a) {

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

    shell.setSize(250, 200);/* w w  w  . j  av  a 2s .  co m*/

    shell.setLayout(new GridLayout(1, false));

    Composite sash = new Composite(shell, SWT.NONE);
    sash.setLayout(new FillLayout());
    sash.setLayoutData(new GridData(GridData.FILL_BOTH));
    final SashForm sashForm = new SashForm(sash, SWT.HORIZONTAL);

    sashForm.SASH_WIDTH = 5;

    sashForm.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));

    final Button one = new Button(sashForm, SWT.PUSH);
    one.setText("One");
    one.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            maximizeHelper(one, sashForm);
        }
    });

    final Button two = new Button(sashForm, SWT.PUSH);
    two.setText("Two");
    two.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            maximizeHelper(two, sashForm);
        }
    });

    final Button three = new Button(sashForm, SWT.PUSH);
    three.setText("Three");
    three.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            maximizeHelper(three, sashForm);
        }
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 118");
    shell.setSize(150, 150);// w  ww .j  a va2  s . c om
    final Cursor[] cursor = new Cursor[1];
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Change cursor");
    Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    button.setSize(size);
    button.addListener(SWT.Selection, e -> {
        FileDialog dialog = new FileDialog(shell);
        dialog.setFilterExtensions(new String[] { "*.ico", "*.gif", "*.*" });
        String name = dialog.open();
        if (name == null)
            return;
        ImageData image = new ImageData(name);
        Cursor oldCursor = cursor[0];
        cursor[0] = new Cursor(display, image, 0, 0);
        shell.setCursor(cursor[0]);
        if (oldCursor != null)
            oldCursor.dispose();
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (cursor[0] != null)
        cursor[0].dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 20");
    CoolBar bar = new CoolBar(shell, SWT.BORDER);
    for (int i = 0; i < 2; i++) {
        CoolItem item = new CoolItem(bar, SWT.NONE);
        Button button = new Button(bar, SWT.PUSH);
        button.setText("Button " + i);
        Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        item.setPreferredSize(item.computeSize(size.x, size.y));
        item.setControl(button);//from   ww w .  j  a  va 2  s  . c  o m
    }
    Rectangle clientArea = shell.getClientArea();
    bar.setLocation(clientArea.x, clientArea.y);
    bar.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

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

        /* Take the screen shot */
        GC gc = new GC(display);
        final Image image = new Image(display, display.getBounds());
        gc.copyArea(image, 0, 0);//from  w  w  w.  jav  a2s  . 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, 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(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.Snippet206.java

public static void main(String[] args) {
    Display display = new Display();
    Image image = display.getSystemImage(SWT.ICON_QUESTION);
    Shell shell = new Shell(display);
    shell.setText("Snippet 206");
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setImage(image);//  ww w.j a  v  a2  s  . com
    button.setText("Button");
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}