Example usage for org.eclipse.swt.widgets Button setSelection

List of usage examples for org.eclipse.swt.widgets Button setSelection

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button setSelection.

Prototype

public void setSelection(boolean selected) 

Source Link

Document

Sets the selection state of the receiver, if it is of type CHECK, RADIO, or TOGGLE.

Usage

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);

    Button b2 = new Button(shell, SWT.CHECK);
    b2.setText("State 2");
    b2.setSelection(false);//from  w w w  . j  a va2  s.co  m

    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: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);
    }//  w  w  w  . j  av  a2  s.co  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.Snippet266.java

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

    TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
    tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

    TabItem item = new TabItem(tabFolder, SWT.NONE);
    item.setText("Widget");
    Composite composite = new Composite(tabFolder, SWT.NONE);
    composite.setLayout(new GridLayout());
    Tree tree = new Tree(composite, SWT.BORDER);
    item.setControl(composite);//from  w  w  w  .  jav  a 2s  . c  om
    tree.setHeaderVisible(true);
    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
    column1.setText("Standard");
    TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
    column2.setText("Widget");
    TreeItem branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Efficient", "Portable" });
    TreeItem leaf = new TreeItem(branch, SWT.NONE);
    leaf.setText(new String[] { "Cross", "Platform" });
    branch.setExpanded(true);
    branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Native", "Controls" });
    leaf = new TreeItem(branch, SWT.NONE);
    leaf.setText(new String[] { "Cross", "Platform" });
    branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Cross", "Platform" });
    column1.pack();
    column2.pack();

    item = new TabItem(tabFolder, SWT.NONE);
    item.setText("Toolkit");

    Button button = new Button(shell, SWT.CHECK);
    button.setText("Totally");
    button.setSelection(true);
    button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

    button = new Button(shell, SWT.PUSH);
    button.setText("Awesome");
    button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

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

From source file:Snippet115.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    Composite c1 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c1.setLayout(new RowLayout());
    Composite c2 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c2.setLayout(new RowLayout());
    final Composite[] composites = new Composite[] { c1, c2 };
    Listener radioGroup = new Listener() {
        public void handleEvent(Event event) {
            for (int i = 0; i < composites.length; i++) {
                Composite composite = composites[i];
                Control[] children = composite.getChildren();
                for (int j = 0; j < children.length; j++) {
                    Control child = children[j];
                    if (child instanceof Button) {
                        Button button = (Button) child;
                        if ((button.getStyle() & SWT.RADIO) != 0)
                            button.setSelection(false);
                    }/*from  ww w . jav a  2 s  . co  m*/
                }
            }
            Button button = (Button) event.widget;
            button.setSelection(true);
        }
    };
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c1, SWT.RADIO);
        button.setText("Button " + i);
        button.addListener(SWT.Selection, radioGroup);
    }
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c2, SWT.RADIO);
        button.setText("Button " + (i + 4));
        button.addListener(SWT.Selection, radioGroup);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 115");
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    Composite c1 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c1.setLayout(new RowLayout());
    Composite c2 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c2.setLayout(new RowLayout());
    final Composite[] composites = new Composite[] { c1, c2 };
    Listener radioGroup = event -> {/*from  w ww  .  ja  va  2  s  .  co m*/
        for (int i = 0; i < composites.length; i++) {
            Composite composite = composites[i];
            Control[] children = composite.getChildren();
            for (int j = 0; j < children.length; j++) {
                Control child = children[j];
                if (child instanceof Button) {
                    Button button1 = (Button) child;
                    if ((button1.getStyle() & SWT.RADIO) != 0)
                        button1.setSelection(false);
                }
            }
        }
        Button button2 = (Button) event.widget;
        button2.setSelection(true);
    };
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c1, SWT.RADIO);
        button.setText("Button " + i);
        button.addListener(SWT.Selection, radioGroup);
    }
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c2, SWT.RADIO);
        button.setText("Button " + (i + 4));
        button.addListener(SWT.Selection, radioGroup);
    }
    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   w  ww  .j av  a 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.Snippet169.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 169");
    shell.setLayout(new FillLayout());
    Listener listener = e -> {/*w ww  .  ja v  a  2s .  c  o  m*/
        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);
            }
        }
        ((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.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  w  ww.  j  a  va  2s.c  o  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.Snippet175.java

public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 175");
    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  ww w.java  2  s . c  o m
    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, e -> {
        Button b1 = (Button) e.widget;
        GridData data1 = (GridData) bHidden.getLayoutData();
        data1.exclude = b1.getSelection();
        bHidden.setVisible(!data1.exclude);
        shell.layout(false);
    });
    shell.setSize(400, 400);
    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  w w .ja va  2s  .c  om
    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();
}