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

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

Introduction

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

Prototype

public void setText(String text) 

Source Link

Document

Sets the receiver's text.

Usage

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  ww  .j  a  va 2 s.  co 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: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);/*from   ww  w  .j a v  a 2  s . co 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:StackLayoutTest.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    StackLayout layout = new StackLayout();
    shell.setLayout(layout);// ww w  .jav a  2s . co  m
    StackLayoutSelectionAdapter adapter = new StackLayoutSelectionAdapter(shell, layout);
    Button one = new Button(shell, SWT.PUSH);
    one.setText("one");
    one.addSelectionListener(adapter);
    Button two = new Button(shell, SWT.PUSH);
    two.setText("two");
    two.addSelectionListener(adapter);
    Button three = new Button(shell, SWT.PUSH);
    three.setText("three");
    three.addSelectionListener(adapter);
    layout.topControl = one;
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:GridLayoutVerticalAlignment.java

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

    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;//from w w  w.j a va 2  s  .  co  m
    gridLayout.makeColumnsEqualWidth = true;

    shell.setLayout(gridLayout);

    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1"); // Default alignment

    List list = new List(shell, SWT.BORDER);
    list.add("item 1");
    list.add("item 2");
    list.add("item 3");
    list.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));

    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button #2");
    button2.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_END));

    Button button3 = new Button(shell, SWT.PUSH);
    button3.setText("3");
    button3.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));

    shell.setSize(450, 400);
    shell.open();

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

From source file:GridLayoutAlignmentHoriVerical.java

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

    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;//from  w  w w .  j  a v a2  s.  c  om
    gridLayout.makeColumnsEqualWidth = true;

    shell.setLayout(gridLayout);

    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1"); // Default alignment

    List list = new List(shell, SWT.BORDER);
    list.add("item 1");
    list.add("item 2");
    list.add("item 3");
    list.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));

    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button #2");
    button2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));

    Button button3 = new Button(shell, SWT.PUSH);
    button3.setText("3");
    button3.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    shell.setSize(450, 400);
    shell.open();

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 116");
    shell.setLayout(new GridLayout());
    Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
    text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    text.setText("Here is some text");
    text.addSelectionListener(widgetDefaultSelectedAdapter(
            e -> System.out.println("Text default selected (overrides default button)")));
    text.addTraverseListener(e -> {//from   ww w .  jav  a 2 s. c om
        if (e.detail == SWT.TRAVERSE_RETURN) {
            e.doit = false;
            e.detail = SWT.TRAVERSE_NONE;
        }
    });
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Ok");
    button.addSelectionListener(widgetSelectedAdapter(e -> System.out.println("Button selected")));
    shell.setDefaultButton(button);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet118.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(150, 150);// ww w. j  a va 2  s  . co  m
    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, new Listener() {
        public void handleEvent(Event 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: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);
                }// ww  w  .  j ava2 s.c om
            }
            ((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.Snippet147.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 147");
    shell.setLayout(new GridLayout());
    Combo combo = new Combo(shell, SWT.NONE);
    combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    combo.setText("Here is some text");
    combo.addSelectionListener(widgetDefaultSelectedAdapter(
            e -> System.out.println("Combo default selected (overrides default button)")));
    combo.addTraverseListener(e -> {//from   w  w  w  . j a  v a  2 s.c  o  m
        if (e.detail == SWT.TRAVERSE_RETURN) {
            e.doit = false;
            e.detail = SWT.TRAVERSE_NONE;
        }
    });
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Ok");
    button.addSelectionListener(widgetSelectedAdapter(e -> System.out.println("Button selected")));
    shell.setDefaultButton(button);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 164");
    shell.setLayout(new GridLayout());
    Image image = new Image(display, Snippet164.class.getResourceAsStream("eclipse.png"));

    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("&Typical button");

    Button button2 = new Button(shell, SWT.PUSH);
    button2.setImage(image);// ww w.  j ava  2  s .  c o  m
    button2.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getName(AccessibleEvent e) {
            e.result = "Eclipse logo";
        }
    });

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