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

public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);

    s.setSize(250, 200);/* w  w  w.  j a v a  2s. com*/
    GridLayout gl = new GridLayout();
    gl.numColumns = 4;
    s.setLayout(gl);

    final Table t = new Table(s, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION);
    final GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 4;
    t.setLayoutData(gd);
    t.setHeaderVisible(true);
    final TableColumn tc1 = new TableColumn(t, SWT.LEFT);
    final TableColumn tc2 = new TableColumn(t, SWT.CENTER);
    final TableColumn tc3 = new TableColumn(t, SWT.CENTER);
    tc1.setText("First Name");
    tc2.setText("Last Name");
    tc3.setText("Address");
    tc1.setWidth(70);
    tc2.setWidth(70);
    tc3.setWidth(80);
    TableItem item1 = new TableItem(t, SWT.NONE);
    item1.setText(new String[] { "A", "B", "Address 1" });
    TableItem item2 = new TableItem(t, SWT.NONE);
    item2.setText(new String[] { "C", "D", "Address 2" });
    TableItem item3 = new TableItem(t, SWT.NONE);
    item3.setText(new String[] { "E", "F", "Address 3" });

    final Text find = new Text(s, SWT.SINGLE | SWT.BORDER);
    final Text replace = new Text(s, SWT.SINGLE | SWT.BORDER);
    final Button replaceBtn = new Button(s, SWT.BORDER | SWT.PUSH);
    replaceBtn.setText("Replace");
    replaceBtn.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TableItem[] tia = t.getItems();

            for (int i = 0; i < tia.length; i++) {
                if (tia[i].getText(2).equals(find.getText())) {
                    tia[i].setText(2, replace.getText());
                }
            }
        }
    });

    s.open();
    while (!s.isDisposed()) {
        if (!d.readAndDispatch())
            d.sleep();
    }
    d.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 a v  a  2 s . c o m

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

    Composite buttonBar = new Composite(shell, SWT.NONE);
    buttonBar.setLayout(new RowLayout());
    Button flip = new Button(buttonBar, SWT.PUSH);
    flip.setText("Switch Orientation");
    Button weights = new Button(buttonBar, SWT.PUSH);
    weights.setText("Restore Weights");

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

    final Button two = new Button(sashForm, SWT.PUSH);
    two.setText("Two");

    final Button three = new Button(sashForm, SWT.PUSH);
    three.setText("Three");

    sashForm.setWeights(new int[] { 2, 2, 2 });

    flip.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            switch (sashForm.getOrientation()) {
            case SWT.HORIZONTAL:
                sashForm.setOrientation(SWT.VERTICAL);
                break;
            case SWT.VERTICAL:
                sashForm.setOrientation(SWT.HORIZONTAL);
                break;
            }
        }
    });

    weights.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            sashForm.setWeights(new int[] { 2, 2, 2 });
        }
    });

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

From source file:MainClass.java

public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);

    s.setSize(250, 200);/* w w w .ja  va 2  s. c  o m*/
    GridLayout gl = new GridLayout();
    gl.numColumns = 4;
    s.setLayout(gl);

    final Table t = new Table(s, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION);
    final GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 4;
    t.setLayoutData(gd);
    t.setHeaderVisible(true);
    final TableColumn tc1 = new TableColumn(t, SWT.LEFT);
    final TableColumn tc2 = new TableColumn(t, SWT.CENTER);
    final TableColumn tc3 = new TableColumn(t, SWT.CENTER);
    tc1.setText("First Name");
    tc2.setText("Last Name");
    tc3.setText("Address");
    tc1.setWidth(70);
    tc2.setWidth(70);
    tc3.setWidth(80);
    TableItem item1 = new TableItem(t, SWT.NONE);
    item1.setText(new String[] { "A", "B", "Address 1" });
    TableItem item2 = new TableItem(t, SWT.NONE);
    item2.setText(new String[] { "C", "D", "Address 2" });
    TableItem item3 = new TableItem(t, SWT.NONE);
    item3.setText(new String[] { "E", "F", "Address 3" });

    final Text find = new Text(s, SWT.SINGLE | SWT.BORDER);
    final Text replace = new Text(s, SWT.SINGLE | SWT.BORDER);
    final Button replaceBtn = new Button(s, SWT.BORDER | SWT.PUSH);
    replaceBtn.setText("Replace");
    replaceBtn.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TableItem[] tia = t.getItems();

            for (int i = 0; i < tia.length; i++) {
                if (tia[i].getText(2).equals(find.getText())) {
                    tia[i].setText(2, replace.getText());
                    tia[i].setBackground(new Color(d, 127, 178, 127));
                }
            }
        }
    });

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

From source file:DialogLayoutDemo.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("This is a long text string that will wrap when the dialog is resized.");
    List list = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    list.setItems(new String[] { "Item 1", "Item 2" });
    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;//from www .j  a v  a 2  s . com
    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:MouseEventPost.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);/*  w w w  . ja  v  a  2 s  .c  o m*/
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
        }
    });
    final Point pt = display.map(shell, null, 50, 50);
    new Thread() {
        Event event;

        public void run() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event = new Event();
            event.type = SWT.MouseMove;
            event.x = pt.x;
            event.y = pt.y;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 193");
    shell.setLayout(new RowLayout(SWT.HORIZONTAL));
    final Tree tree = new Tree(shell, SWT.BORDER | SWT.CHECK);
    tree.setLayoutData(new RowData(-1, 300));
    tree.setHeaderVisible(true);//from   www  .j  av a2s  .  c o  m
    TreeColumn column = new TreeColumn(tree, SWT.LEFT);
    column.setText("Column 0");
    column = new TreeColumn(tree, SWT.CENTER);
    column.setText("Column 1");
    column = new TreeColumn(tree, SWT.LEFT);
    column.setText("Column 2");
    column = new TreeColumn(tree, SWT.RIGHT);
    column.setText("Column 3");
    column = new TreeColumn(tree, SWT.CENTER);
    column.setText("Column 4");
    for (int i = 0; i < 5; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        String[] text = new String[] { i + ":0", i + ":1", i + ":2", i + ":3", i + ":4" };
        item.setText(text);
        for (int j = 0; j < 5; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            text = new String[] { i + "," + j + ":0", i + "," + j + ":1", i + "," + j + ":2",
                    i + "," + j + ":3", i + "," + j + ":4" };
            subItem.setText(text);
            for (int k = 0; k < 5; k++) {
                TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
                text = new String[] { i + "," + j + "," + k + ":0", i + "," + j + "," + k + ":1",
                        i + "," + j + "," + k + ":2", i + "," + j + "," + k + ":3",
                        i + "," + j + "," + k + ":4" };
                subsubItem.setText(text);
            }
        }
    }
    Listener listener = e -> System.out.println("Move " + e.widget);
    TreeColumn[] columns = tree.getColumns();
    for (int i = 0; i < columns.length; i++) {
        columns[i].setWidth(100);
        columns[i].setMoveable(true);
        columns[i].addListener(SWT.Move, listener);
    }
    Button b = new Button(shell, SWT.PUSH);
    b.setText("invert column order");
    b.addListener(SWT.Selection, e -> {
        int[] order = tree.getColumnOrder();
        for (int i = 0; i < order.length / 2; i++) {
            int temp = order[i];
            order[i] = order[order.length - i - 1];
            order[order.length - i - 1] = temp;
        }
        tree.setColumnOrder(order);
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TreeEditorButton.java

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

    final Tree tree = new Tree(shell, SWT.SINGLE);
    for (int i = 0; i < 3; i++) {
        TreeItem iItem = new TreeItem(tree, SWT.NONE);
        iItem.setText("Item " + (i + 1));
        for (int j = 0; j < 3; j++) {
            TreeItem jItem = new TreeItem(iItem, SWT.NONE);
            jItem.setText("Sub Item " + (j + 1));
            for (int k = 0; k < 3; k++) {
                new TreeItem(jItem, SWT.NONE).setText("Sub Sub Item " + (k + 1));
            }//from   ww w. jav  a 2 s  .c  om
            jItem.setExpanded(true);
        }
        iItem.setExpanded(true);
    }

    final TreeEditor editor = new TreeEditor(tree);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;

    tree.addMouseListener(new MouseAdapter() {
        public void mouseDown(MouseEvent event) {
            final TreeItem item = tree.getSelection()[0];

            final Button bn = new Button(tree, SWT.NONE);
            bn.setText("click");
            bn.setFocus();

            bn.addSelectionListener(new SelectionListener() {

                public void widgetSelected(SelectionEvent arg0) {
                    int style = SWT.ICON_QUESTION | SWT.YES | SWT.NO;

                    MessageBox messageBox = new MessageBox(shell, style);
                    messageBox.setMessage("Message");
                    int rc = messageBox.open();

                    item.setText(rc + "");
                    bn.dispose();
                }

                public void widgetDefaultSelected(SelectionEvent arg0) {
                }
            });

            editor.setEditor(bn, item);
        }
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 172");
    GridLayout layout = new GridLayout(4, false);
    shell.setLayout(layout);/*www  .  j av  a 2  s  . com*/

    Button b = new Button(shell, SWT.PUSH);
    b.setText("LEFT, TOP");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("LEFT, CENTER");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("LEFT, BOTTOM");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("LEFT, FILL");
    b.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("CENTER, TOP");
    b.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("CENTER, CENTER");
    b.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("CENTER, BOTTOM");
    b.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("CENTER, FILL");
    b.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("RIGHT, TOP");
    b.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("RIGHT, CENTER");
    b.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("RIGHT, BOTTOM");
    b.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("RIGHT, FILL");
    b.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("FILL, TOP");
    b.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("FILL, CENTER");
    b.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("FILL, BOTTOM");
    b.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, true, 1, 1));
    b = new Button(shell, SWT.PUSH);
    b.setText("FILL, FILL");
    b.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

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

From source file:BusyCursorDisplay.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));
    final int[] nextId = new int[1];
    Button b = new Button(shell, SWT.PUSH);
    b.setText("invoke long running job");
    b.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Runnable longJob = new Runnable() {
                boolean done = false;

                int id;

                public void run() {
                    Thread thread = new Thread(new Runnable() {
                        public void run() {
                            id = nextId[0]++;
                            display.syncExec(new Runnable() {
                                public void run() {
                                    if (text.isDisposed())
                                        return;
                                    text.append("\nStart long running task " + id);
                                }// w w  w  . j  a va  2s  .com
                            });
                            for (int i = 0; i < 100000; i++) {
                                if (display.isDisposed())
                                    return;
                                System.out.println("do task that takes a long time in a separate thread " + id);
                            }
                            if (display.isDisposed())
                                return;
                            display.syncExec(new Runnable() {
                                public void run() {
                                    if (text.isDisposed())
                                        return;
                                    text.append("\nCompleted long running task " + id);
                                }
                            });
                            done = true;
                            display.wake();
                        }
                    });
                    thread.start();
                    while (!done && !shell.isDisposed()) {
                        if (!display.readAndDispatch())
                            display.sleep();
                    }
                }
            };
            BusyIndicator.showWhile(display, longJob);
        }
    });
    shell.setSize(250, 150);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet142.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);/*from   w w w.jav  a2 s . co m*/
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
        }
    });
    final Point pt = display.map(shell, null, 50, 50);
    new Thread() {
        Event event;

        public void run() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event = new Event();
            event.type = SWT.MouseMove;
            event.x = pt.x;
            event.y = pt.y;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseDown;
            event.button = 1;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseUp;
            display.post(event);
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}