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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, true));
    shell.setText("One Potato, Two Potato");
    // Create the focus listener
    FocusListener listener = new FocusListener() {
        public void focusGained(FocusEvent event) {
            Button button = (Button) event.getSource();
            button.setText("I'm It!");
        }//from ww w  .  ja  v  a2s.c o m

        public void focusLost(FocusEvent event) {
            Button button = (Button) event.getSource();
            button.setText("Pick Me!");
        }
    };

    // Create the buttons and add the listener to each one
    for (int i = 0; i < 6; i++) {
        Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        button.setText("Pick Me!");
        button.addFocusListener(listener);
    }

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

From source file:GridLayoutSpanVeriticalHorizontal.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  www . ja v  a 2  s.  c  o 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");
    GridData gridData = new GridData(GridData.VERTICAL_ALIGN_END);
    gridData.horizontalSpan = 2;
    gridData.horizontalAlignment = GridData.FILL;
    button2.setLayoutData(gridData);

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

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

From source file:Snippet144.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    final Table table = new Table(shell, SWT.VIRTUAL | SWT.BORDER);
    table.addListener(SWT.SetData, new Listener() {
        public void handleEvent(Event event) {
            TableItem item = (TableItem) event.item;
            int index = table.indexOf(item);
            item.setText("Item " + index);
            System.out.println(item.getText());
        }/*  w w w . java 2 s. co m*/
    });
    table.setLayoutData(new RowData(200, 200));
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Add Items");
    final Label label = new Label(shell, SWT.NONE);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            long t1 = System.currentTimeMillis();
            table.setItemCount(COUNT);
            long t2 = System.currentTimeMillis();
            label.setText("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms)");
            shell.layout();
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 299");
    RowLayout layout = new RowLayout();
    layout.center = true;/* www  .j a va 2s  .c o m*/
    shell.setLayout(layout);

    Button button0 = new Button(shell, SWT.PUSH);
    button0.setText("Button 0");

    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Button 1");
    button1.setLayoutData(new RowData(SWT.DEFAULT, 50));

    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Button 2");
    button2.setLayoutData(new RowData(SWT.DEFAULT, 70));

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

    shell.pack();
    shell.open();

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 6");
    shell.setLayout(new GridLayout());
    final Composite c = new Composite(shell, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;/* w  ww . j  av  a  2 s  .  c o  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, 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:Snippet116.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    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(new SelectionAdapter() {
        public void widgetDefaultSelected(SelectionEvent e) {
            System.out.println("Text default selected (overrides default button)");
        }/*from w w  w  . ja  v  a2 s  . com*/
    });
    text.addTraverseListener(new TraverseListener() {
        public void keyTraversed(TraverseEvent e) {
            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(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent 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:MainClass.java

public static void main(String[] a) {

    Shell shell = new Shell(new Display());
    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
    dialog.setText("Delete File");
    dialog.setSize(250, 150);//from   w  w w.  ja  v a  2s  .com

    final Button buttonOK = new Button(dialog, SWT.PUSH);
    buttonOK.setText("OK");
    buttonOK.setBounds(20, 55, 80, 25);

    Button buttonCancel = new Button(dialog, SWT.PUSH);
    buttonCancel.setText("Cancel");
    buttonCancel.setBounds(120, 55, 80, 25);

    final Label label = new Label(dialog, SWT.NONE);
    label.setText("Delete the file?");
    label.setBounds(20, 15, 100, 20);

    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            if (event.widget == buttonOK) {
                System.out.println("OK");
            } else {
                System.out.println("Cancel");
            }
            dialog.close();
        }
    };

    buttonOK.addListener(SWT.Selection, listener);
    buttonCancel.addListener(SWT.Selection, listener);

    dialog.open();

}

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);//from w w w  .  ja v  a2 s  .com
    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.Snippet144.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 144");
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    final Table table = new Table(shell, SWT.VIRTUAL | SWT.BORDER);
    table.addListener(SWT.SetData, event -> {
        TableItem item = (TableItem) event.item;
        int index = table.indexOf(item);
        item.setText("Item " + index);
        System.out.println(item.getText());
    });/* w  w  w.j  av  a2  s .co  m*/
    table.setLayoutData(new RowData(200, 200));
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Add Items");
    final Label label = new Label(shell, SWT.NONE);
    button.addListener(SWT.Selection, event -> {
        long t1 = System.currentTimeMillis();
        table.setItemCount(COUNT);
        long t2 = System.currentTimeMillis();
        label.setText("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms)");
        shell.layout();
    });
    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 -> {/*from w  w  w.jav a 2 s.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();
}