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

public static void main(String[] args) {
    Display display = new Display();
    final Clipboard cb = new Clipboard(display);
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);

    Button copy = new Button(shell, SWT.PUSH);
    copy.setText("Copy");
    copy.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            String textData = text.getSelectionText();
            TextTransfer textTransfer = TextTransfer.getInstance();
            cb.setContents(new Object[] { textData }, new Transfer[] { textTransfer });
        }/*from  w ww . j  a  v a2s.c o  m*/
    });

    Button paste = new Button(shell, SWT.PUSH);
    paste.setText("Paste");
    paste.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            TextTransfer transfer = TextTransfer.getInstance();
            String data = (String) cb.getContents(transfer);
            if (data != null) {
                text.insert(data);
            }
        }
    });

    FormData data = new FormData();
    data.right = new FormAttachment(100, -5);
    data.top = new FormAttachment(0, 5);
    copy.setLayoutData(data);

    data = new FormData();
    data.right = new FormAttachment(100, -5);
    data.top = new FormAttachment(copy, 5);
    paste.setLayoutData(data);

    data = new FormData();
    data.left = new FormAttachment(0, 5);
    data.top = new FormAttachment(0, 5);
    data.right = new FormAttachment(copy, -5);
    data.bottom = new FormAttachment(100, -5);
    text.setLayoutData(data);

    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    cb.dispose();
    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;/*from ww  w  . j  av a2 s. c  o m*/
    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:Snippet106.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
    table.setHeaderVisible(true);//from   ww w  . j a va  2 s  . c  o m
    for (int i = 0; i < 4; i++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Column " + i);
    }
    final TableColumn[] columns = table.getColumns();
    for (int i = 0; i < 12; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        for (int j = 0; j < columns.length; j++) {
            item.setText(j, "Item " + i);
        }
    }
    for (int i = 0; i < columns.length; i++)
        columns[i].pack();
    Button button = new Button(shell, SWT.PUSH);
    final int index = 1;
    button.setText("Insert Column " + index + "a");
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            TableColumn column = new TableColumn(table, SWT.NONE, index);
            column.setText("Column " + index + "a");
            TableItem[] items = table.getItems();
            for (int i = 0; i < items.length; i++) {
                items[i].setText(index, "Item " + i + "a");
            }
            column.pack();
        }
    });
    shell.pack();
    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);/*from  w  ww .ja  va  2  s  .c o 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:GridLayoutComplex.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;/*from   www  . j av a2  s.co m*/
    layout.makeColumnsEqualWidth = true;
    shell.setLayout(layout);

    // Create the big button in the upper left
    GridData data = new GridData(GridData.FILL_BOTH);
    data.widthHint = 200;
    Button one = new Button(shell, SWT.PUSH);
    one.setText("one");
    one.setLayoutData(data);

    // Create a composite to hold the three buttons in the upper right
    Composite composite = new Composite(shell, SWT.NONE);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 2;
    composite.setLayoutData(data);
    layout = new GridLayout();
    layout.numColumns = 1;
    layout.marginHeight = 15;
    composite.setLayout(layout);

    // Create button "two"
    data = new GridData(GridData.FILL_BOTH);
    Button two = new Button(composite, SWT.PUSH);
    two.setText("two");
    two.setLayoutData(data);

    // Create button "three"
    data = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
    Button three = new Button(composite, SWT.PUSH);
    three.setText("three");
    three.setLayoutData(data);

    // Create button "four"
    data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    Button four = new Button(composite, SWT.PUSH);
    four.setText("four");
    four.setLayoutData(data);

    // Create the long button across the bottom
    data = new GridData();
    data.horizontalAlignment = GridData.FILL;
    data.grabExcessHorizontalSpace = true;
    data.horizontalSpan = 3;
    data.heightHint = 150;
    Button five = new Button(shell, SWT.PUSH);
    five.setText("five");
    five.setLayoutData(data);

    shell.pack();
    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);//from   w w w .j  a  v  a2 s  .c  o  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:ScrollBarSelectionListener.java

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

    final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button button1 = new Button(sc1, SWT.PUSH);
    button1.setText("Button 1");
    button1.setSize(400, 300);/*from   w  w  w . j  a va2  s  .c  o  m*/
    sc1.setContent(button1);

    final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button button2 = new Button(sc2, SWT.PUSH);
    button2.setText("Button 2");
    button2.setSize(300, 400);
    sc2.setContent(button2);

    final ScrollBar vBar1 = sc1.getVerticalBar();
    final ScrollBar vBar2 = sc2.getVerticalBar();
    final ScrollBar hBar1 = sc1.getHorizontalBar();
    final ScrollBar hBar2 = sc2.getHorizontalBar();
    SelectionListener listener1 = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            int x = hBar1.getSelection() * (hBar2.getMaximum() - hBar2.getThumb())
                    / Math.max(1, hBar1.getMaximum() - hBar1.getThumb());
            int y = vBar1.getSelection() * (vBar2.getMaximum() - vBar2.getThumb())
                    / Math.max(1, vBar1.getMaximum() - vBar1.getThumb());
            sc2.setOrigin(x, y);
        }
    };
    SelectionListener listener2 = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            int x = hBar2.getSelection() * (hBar1.getMaximum() - hBar1.getThumb())
                    / Math.max(1, hBar2.getMaximum() - hBar2.getThumb());
            int y = vBar2.getSelection() * (vBar1.getMaximum() - vBar1.getThumb())
                    / Math.max(1, vBar2.getMaximum() - vBar2.getThumb());
            sc1.setOrigin(x, y);
        }
    };
    vBar1.addSelectionListener(listener1);
    hBar1.addSelectionListener(listener1);
    vBar2.addSelectionListener(listener2);
    hBar2.addSelectionListener(listener2);

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

From source file:Snippet172.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    GridLayout layout = new GridLayout(4, false);
    shell.setLayout(layout);/*from  w ww .  ja v  a2 s.c o  m*/

    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:Snippet65.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", "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;/*from   w ww. j a va  2 s  . c  o  m*/
    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:org.eclipse.swt.snippets.Snippet94.java

public static void main(String[] args) {
    Display display = new Display();
    final Clipboard cb = new Clipboard(display);
    final Shell shell = new Shell(display);
    shell.setText("Snippet 94");
    shell.setLayout(new FormLayout());
    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);

    Button copy = new Button(shell, SWT.PUSH);
    copy.setText("Copy");
    copy.addListener(SWT.Selection, e -> {
        String textData = text.getSelectionText();
        if (textData.length() > 0) {
            TextTransfer textTransfer = TextTransfer.getInstance();
            cb.setContents(new Object[] { textData }, new Transfer[] { textTransfer });
        }//from   w  ww .j av  a 2  s .  co  m
    });

    Button paste = new Button(shell, SWT.PUSH);
    paste.setText("Paste");
    paste.addListener(SWT.Selection, e -> {
        TextTransfer transfer = TextTransfer.getInstance();
        String data = (String) cb.getContents(transfer);
        if (data != null) {
            text.insert(data);
        }
    });

    FormData data = new FormData();
    data.left = new FormAttachment(paste, 0, SWT.LEFT);
    data.right = new FormAttachment(100, -5);
    data.top = new FormAttachment(0, 5);
    copy.setLayoutData(data);

    data = new FormData();
    data.right = new FormAttachment(100, -5);
    data.top = new FormAttachment(copy, 5);
    paste.setLayoutData(data);

    data = new FormData();
    data.left = new FormAttachment(0, 5);
    data.top = new FormAttachment(0, 5);
    data.right = new FormAttachment(paste, -5);
    data.bottom = new FormAttachment(100, -5);
    text.setLayoutData(data);

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