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:org.eclipse.swt.snippets.Snippet254.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 254");
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.fill = true;/*from  ww  w  .  j ava 2  s  .co  m*/
    layout.wrap = false;
    shell.setLayout(layout);
    final Tree tree = new Tree(shell, SWT.NONE);
    for (int i = 0; i < 32; i++) {
        TreeItem item0 = new TreeItem(tree, SWT.NONE);
        item0.setText("Item " + i + " is quite long");
        for (int j = 0; j < 3; j++) {
            TreeItem item1 = new TreeItem(item0, SWT.NONE);
            item1.setText("Item " + i + " " + j + " is quite long");
            for (int k = 0; k < 3; k++) {
                TreeItem item2 = new TreeItem(item1, SWT.NONE);
                item2.setText("Item " + i + " " + j + " " + k + " is quite long");
                for (int l = 0; l < 3; l++) {
                    TreeItem item3 = new TreeItem(item2, SWT.NONE);
                    item3.setText("Item " + i + " " + j + " " + k + " " + l + " is quite long");
                }
            }
        }
    }
    tree.setLayoutData(new RowData(200, 200));
    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Visible Items []");
    button.addListener(SWT.Selection, e -> {
        int visibleCount = 0;
        Rectangle rect = tree.getClientArea();
        TreeItem item = tree.getTopItem();
        while (item != null) {
            visibleCount++;
            Rectangle itemRect = item.getBounds();
            if (itemRect.y + itemRect.height > rect.y + rect.height) {
                break;
            }
            item = nextItem(tree, item);
        }
        button.setText("Visible Items [" + visibleCount + "]");
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:DialogClass.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Dialog Example");
    shell.setSize(300, 200);/*from ww  w.  ja v a 2 s.  c o m*/
    shell.open();

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Delete File");
    button.setBounds(20, 40, 80, 25);

    final Text text = new Text(shell, SWT.SHADOW_IN);
    text.setBounds(140, 40, 100, 25);

    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
    dialog.setText("Delete File");
    dialog.setSize(250, 150);

    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) {
                deleteFlag = true;
            } else {
                deleteFlag = false;
            }
            dialog.close();
        }
    };

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

    Listener buttonListener = new Listener() {
        public void handleEvent(Event event) {
            dialog.open();
        }
    };

    button.addListener(SWT.Selection, buttonListener);

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

    if (deleteFlag) {
        text.setText("File deleted.");
    } else {
        text.setText("File not deleted.");
    }

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

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

public static void main(String[] args) {
    final String SEARCH_STRING = "4b";

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 287");
    shell.setBounds(10, 10, 300, 300);//from  w w  w  .  j a  v a  2  s  .  c om
    shell.setLayout(new GridLayout());

    /* create the Tree */
    tree = new Tree(shell, SWT.FULL_SELECTION);
    tree.setLinesVisible(true);
    tree.setLayoutData(new GridData(GridData.FILL_BOTH));
    for (int i = 0; i < 3; i++) {
        new TreeColumn(tree, SWT.NONE).setWidth(90);
    }
    int index = 0;
    for (int i = 0; i < 3; i++) {
        TreeItem item = createItem(null, index++);
        for (int j = 0; j < i; j++) {
            item = createItem(item, index++);
        }
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Find '" + SEARCH_STRING + "'");
    button.addListener(SWT.Selection, event -> {
        int itemCount = tree.getItemCount();
        for (int i = 0; i < itemCount; i++) {
            TreeItem item = tree.getItem(i);
            boolean success = find(item, SEARCH_STRING);
            if (success) {
                System.out.println("Found it");
                return;
            }
        }
        System.out.println("Did not find it");
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet75.java

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

    Composite c1 = new Composite(shell, SWT.BORDER);
    c1.setLayout(new RowLayout());
    Button b1 = new Button(c1, SWT.PUSH);
    b1.setText("B1");
    Button[] radios = new Button[3];
    for (int i = 0; i < radios.length; i++) {
        radios[i] = new Button(c1, SWT.RADIO);
        radios[i].setText("R" + (i == 1 ? "&" : "") + i);
    }/* w ww  .  j  a  v  a2 s  . c o m*/
    Button b2 = new Button(c1, SWT.PUSH);
    b2.setText("B2");
    List l1 = new List(c1, SWT.SINGLE | SWT.BORDER);
    l1.setItems(new String[] { "L1" });
    Button b3 = new Button(c1, SWT.PUSH);
    b3.setText("B&3");
    Button b3_1 = new Button(c1, SWT.PUSH);
    b3_1.setText("B3_1");

    Composite c2 = new Composite(shell, SWT.BORDER);
    c2.setLayout(new RowLayout());
    Button b4 = new Button(c2, SWT.PUSH);
    b4.setText("B&4");
    Button b5 = new Button(c2, SWT.PUSH);
    b5.setText("B&5");

    List l2 = new List(shell, SWT.SINGLE | SWT.BORDER);
    l2.setItems(new String[] { "L2" });

    ToolBar tb1 = new ToolBar(shell, SWT.FLAT | SWT.BORDER);
    ToolItem i1 = new ToolItem(tb1, SWT.RADIO);
    i1.setText("I1");
    ToolItem i2 = new ToolItem(tb1, SWT.RADIO);
    i2.setText("I&2");
    Combo combo1 = new Combo(tb1, SWT.READ_ONLY | SWT.BORDER);
    combo1.setItems(new String[] { "C1" });
    combo1.setText("C1");
    combo1.pack();
    ToolItem i3 = new ToolItem(tb1, SWT.SEPARATOR);
    i3.setWidth(combo1.getSize().x);
    i3.setControl(combo1);
    i3.setText("I3");
    ToolItem i4 = new ToolItem(tb1, SWT.PUSH);
    i4.setText("I4");
    ToolItem i5 = new ToolItem(tb1, SWT.CHECK);
    i5.setText("I5");

    Button b6 = new Button(shell, SWT.PUSH);
    b6.setText("B&6");

    Composite c4 = new Composite(shell, SWT.BORDER);
    c4.setSize(32, 32);
    Composite c5 = new Composite(c4, SWT.BORDER);
    c5.setSize(20, 20);

    Control[] list1 = new Control[] { b2, b1, b3_1, b3 };
    c1.setTabList(list1);
    Control[] list2 = new Control[] { c1, b6, tb1, c4, c2, l2 };
    shell.setTabList(list2);

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

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 317");
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;/*from   w  w  w . ja va2s. co m*/
    shell.setLayout(gridLayout);
    final Text location = new Text(shell, SWT.BORDER);
    GridData data = new GridData();
    data.horizontalAlignment = GridData.FILL;
    data.grabExcessHorizontalSpace = true;
    location.setLayoutData(data);
    Button go = new Button(shell, SWT.PUSH);
    go.setText("Go");

    final Browser browser;
    try {
        browser = new Browser(shell, SWT.NONE);
    } catch (SWTError e) {
        System.out.println("Could not instantiate Browser: " + e.getMessage());
        display.dispose();
        return;
    }
    data = new GridData();
    data.horizontalAlignment = data.verticalAlignment = GridData.FILL;
    data.grabExcessHorizontalSpace = data.grabExcessVerticalSpace = true;
    data.horizontalSpan = 2;
    browser.setLayoutData(data);
    browser.setUrl("eclipse.org");
    browser.addLocationListener(new LocationAdapter() {
        @Override
        public void changed(LocationEvent event) {
            location.setText(event.location);
        }
    });

    Listener navigateListener = event -> browser.setUrl(location.getText());
    go.addListener(SWT.Selection, navigateListener);
    location.addListener(SWT.DefaultSelection, navigateListener);

    browser.addAuthenticationListener(event -> {
        try {
            URL url = new URL(event.location);
            if (url.getHost().equals(KNOWN_HOST)) {
                event.user = KNOWN_USER;
                event.password = KNOWN_PASSWORD;
            } else {
                /* do nothing, let default prompter run */
            }
        } catch (MalformedURLException e) {
            /* should not happen, let default prompter run */
        }
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 142");
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);/*from  w  w w.  j a v  a 2 s.  c o  m*/
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown,
            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;

        @Override
        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();
}

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

public static void main(String[] arg) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Skin example");
    shell.setLayout(new GridLayout());

    Group container = new Group(shell, SWT.None);
    container.setText("Container");
    container.setLayout(new GridLayout(3, false));

    Composite child1 = new Composite(container, SWT.BORDER);
    child1.setLayout(new GridLayout());
    new Label(child1, SWT.NONE).setText("Label in pane 1");

    Composite child2 = new Composite(container, SWT.BORDER);
    child2.setLayout(new GridLayout());
    new Button(child2, SWT.PUSH).setText("Button in pane2");

    final Composite child3 = new Composite(container, SWT.BORDER);
    child3.setLayout(new GridLayout());
    new Text(child3, SWT.BORDER).setText("Text in pane3");

    display.addListener(SWT.Skin, event -> {
        System.out.println("Skin: " + event.widget);
        setBackground(event.display, (Control) event.widget);
    });//ww  w  .  j  a v a2s  .c o m

    Composite buttonContainer = new Composite(shell, SWT.NONE);
    buttonContainer.setLayout(new GridLayout(3, false));
    Button reskin = new Button(buttonContainer, SWT.PUSH);
    reskin.setText("Reskin All");
    reskin.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        shell.reskin(SWT.ALL);
    }));
    Button reskin2 = new Button(buttonContainer, SWT.PUSH);
    reskin2.setText("Reskin Shell");
    reskin2.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        shell.reskin(SWT.None);
    }));
    Button reskin3 = new Button(buttonContainer, SWT.PUSH);
    reskin3.setText("Reskin Right Composite");
    reskin3.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        child3.reskin(SWT.ALL);
    }));

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Embedding objects in text");
    final Image[] images = { new Image(display, 32, 32), new Image(display, 20, 40),
            new Image(display, 40, 20) };
    int[] colors = { SWT.COLOR_BLUE, SWT.COLOR_MAGENTA, SWT.COLOR_GREEN };
    for (int i = 0; i < images.length; i++) {
        GC gc = new GC(images[i]);
        gc.setBackground(display.getSystemColor(colors[i]));
        gc.fillRectangle(images[i].getBounds());
        gc.dispose();/*from   www .  jav  a 2s  . c  o  m*/
    }

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.pack();
    String text = "Here is some text with a blue image \uFFFC, a magenta image \uFFFC, a green image \uFFFC, and a button: \uFFFC.";
    final int[] imageOffsets = { 36, 55, 72 };
    final TextLayout layout = new TextLayout(display);
    layout.setText(text);
    for (int i = 0; i < images.length; i++) {
        Rectangle bounds = images[i].getBounds();
        TextStyle imageStyle = new TextStyle(null, null, null);
        imageStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
        layout.setStyle(imageStyle, imageOffsets[i], imageOffsets[i]);
    }
    Rectangle bounds = button.getBounds();
    TextStyle buttonStyle = new TextStyle(null, null, null);
    buttonStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
    final int buttonOffset = text.length() - 2;
    layout.setStyle(buttonStyle, buttonOffset, buttonOffset);

    shell.addListener(SWT.Paint, event -> {
        GC gc = event.gc;
        Point margin = new Point(10, 10);
        layout.setWidth(shell.getClientArea().width - 2 * margin.x);
        layout.draw(event.gc, margin.x, margin.y);
        for (int i = 0; i < images.length; i++) {
            int offset = imageOffsets[i];
            int lineIndex1 = layout.getLineIndex(offset);
            FontMetrics lineMetrics1 = layout.getLineMetrics(lineIndex1);
            Point point1 = layout.getLocation(offset, false);
            GlyphMetrics glyphMetrics1 = layout.getStyle(offset).metrics;
            gc.drawImage(images[i], point1.x + margin.x,
                    point1.y + margin.y + lineMetrics1.getAscent() - glyphMetrics1.ascent);
        }
        int lineIndex2 = layout.getLineIndex(buttonOffset);
        FontMetrics lineMetrics2 = layout.getLineMetrics(lineIndex2);
        Point point2 = layout.getLocation(buttonOffset, false);
        GlyphMetrics glyphMetrics2 = layout.getStyle(buttonOffset).metrics;
        button.setLocation(point2.x + margin.x,
                point2.y + margin.y + lineMetrics2.getAscent() - glyphMetrics2.ascent);
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    layout.dispose();
    for (int i = 0; i < images.length; i++) {
        images[i].dispose();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Shell");
    FillLayout fillLayout = new FillLayout();
    fillLayout.marginWidth = 10;//from w  w w.  ja va 2 s  .  c o  m
    fillLayout.marginHeight = 10;
    shell.setLayout(fillLayout);

    Button open = new Button(shell, SWT.PUSH);
    open.setText("Prompt for a String");
    open.addSelectionListener(widgetSelectedAdapter(e -> {
        final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        dialog.setText("Dialog Shell");
        FormLayout formLayout = new FormLayout();
        formLayout.marginWidth = 10;
        formLayout.marginHeight = 10;
        formLayout.spacing = 10;
        dialog.setLayout(formLayout);

        Label label = new Label(dialog, SWT.NONE);
        label.setText("Type a String:");
        FormData data = new FormData();
        label.setLayoutData(data);

        Button cancel = new Button(dialog, SWT.PUSH);
        cancel.setText("Cancel");
        data = new FormData();
        data.width = 60;
        data.right = new FormAttachment(100, 0);
        data.bottom = new FormAttachment(100, 0);
        cancel.setLayoutData(data);
        cancel.addSelectionListener(widgetSelectedAdapter(event -> {
            System.out.println("User cancelled dialog");
            dialog.close();
        }));

        final Text text = new Text(dialog, SWT.BORDER);
        data = new FormData();
        data.width = 200;
        data.left = new FormAttachment(label, 0, SWT.DEFAULT);
        data.right = new FormAttachment(100, 0);
        data.top = new FormAttachment(label, 0, SWT.CENTER);
        data.bottom = new FormAttachment(cancel, 0, SWT.DEFAULT);
        text.setLayoutData(data);

        Button ok = new Button(dialog, SWT.PUSH);
        ok.setText("OK");
        data = new FormData();
        data.width = 60;
        data.right = new FormAttachment(cancel, 0, SWT.DEFAULT);
        data.bottom = new FormAttachment(100, 0);
        ok.setLayoutData(data);
        ok.addSelectionListener(widgetSelectedAdapter(event -> {
            System.out.println("User typed: " + text.getText());
            dialog.close();
        }));

        dialog.setDefaultButton(ok);
        dialog.pack();
        dialog.open();
    }));
    shell.pack();
    shell.open();

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

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

public static void main(String[] args) {
    final Display display = new Display();
    Font font = new Font(display, "Tahoma", 16, SWT.NORMAL);
    final Shell shell = new Shell(display);
    shell.setText("Snippet 217");
    shell.setLayout(new GridLayout());
    styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setFont(font);//from  w w w  . ja v a2  s  .c o  m
    styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    styledText.setText(text);
    Button button = new Button(styledText, SWT.PUSH);
    button.setText("Button 1");
    int offset = text.indexOf('\uFFFC');
    addControl(button, offset);
    button.setLocation(styledText.getLocationAtOffset(offset));
    Combo combo = new Combo(styledText, SWT.NONE);
    combo.add("item 1");
    combo.add("another item");
    combo.setText(combo.getItem(0));
    offset = text.indexOf('\uFFFC', offset + 1);
    addControl(combo, offset);
    combo.setLocation(styledText.getLocationAtOffset(offset));

    // use a verify listener to dispose the controls
    styledText.addVerifyListener(event -> {
        if (event.start == event.end)
            return;
        String text = styledText.getText(event.start, event.end - 1);
        int index = text.indexOf('\uFFFC');
        while (index != -1) {
            StyleRange style = styledText.getStyleRangeAtOffset(event.start + index);
            if (style != null) {
                Control control = (Control) style.data;
                if (control != null)
                    control.dispose();
            }
            index = text.indexOf('\uFFFC', index + 1);
        }
    });

    // reposition widgets on paint event
    styledText.addPaintObjectListener(event -> {
        Control control = (Control) event.style.data;
        Point pt = control.getSize();
        int x = event.x + MARGIN;
        int y = event.y + event.ascent - 2 * pt.y / 3;
        control.setLocation(x, y);
    });

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