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.Snippet214.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 214");
    shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
    FillLayout layout1 = new FillLayout(SWT.VERTICAL);
    layout1.marginWidth = layout1.marginHeight = 10;
    shell.setLayout(layout1);//from   www.  j  av a  2s .  c o  m
    Group group = new Group(shell, SWT.NONE);
    group.setText("Group ");
    RowLayout layout2 = new RowLayout(SWT.VERTICAL);
    layout2.marginWidth = layout2.marginHeight = layout2.spacing = 10;
    group.setLayout(layout2);
    for (int i = 0; i < 8; i++) {
        Button button = new Button(group, SWT.RADIO);
        button.setText("Button " + i);
    }
    shell.addListener(SWT.Resize, event -> {
        Rectangle rect = shell.getClientArea();
        Image newImage = new Image(display, Math.max(1, rect.width), 1);
        GC gc = new GC(newImage);
        gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
        gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
        gc.dispose();
        shell.setBackgroundImage(newImage);
        if (oldImage != null)
            oldImage.dispose();
        oldImage = newImage;
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (oldImage != null)
        oldImage.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 115");
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    Composite c1 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c1.setLayout(new RowLayout());
    Composite c2 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c2.setLayout(new RowLayout());
    final Composite[] composites = new Composite[] { c1, c2 };
    Listener radioGroup = event -> {/*from  w  w w.  java  2s .  c o  m*/
        for (int i = 0; i < composites.length; i++) {
            Composite composite = composites[i];
            Control[] children = composite.getChildren();
            for (int j = 0; j < children.length; j++) {
                Control child = children[j];
                if (child instanceof Button) {
                    Button button1 = (Button) child;
                    if ((button1.getStyle() & SWT.RADIO) != 0)
                        button1.setSelection(false);
                }
            }
        }
        Button button2 = (Button) event.widget;
        button2.setSelection(true);
    };
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c1, SWT.RADIO);
        button.setText("Button " + i);
        button.addListener(SWT.Selection, radioGroup);
    }
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c2, SWT.RADIO);
        button.setText("Button " + (i + 4));
        button.addListener(SWT.Selection, radioGroup);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 249");
    Rectangle clientArea = shell.getClientArea();
    shell.setBounds(clientArea.x + 10, clientArea.y + 10, 300, 200);
    // create the composite that the pages will share
    final Composite contentPanel = new Composite(shell, SWT.BORDER);
    contentPanel.setBounds(clientArea.x + 100, clientArea.y + 10, 190, 90);
    final StackLayout layout = new StackLayout();
    contentPanel.setLayout(layout);/*from   w ww  .  ja v  a 2 s  .co m*/

    // create the first page's content
    final Composite page0 = new Composite(contentPanel, SWT.NONE);
    page0.setLayout(new RowLayout());
    Label label = new Label(page0, SWT.NONE);
    label.setText("Label on page 1");
    label.pack();

    // create the second page's content
    final Composite page1 = new Composite(contentPanel, SWT.NONE);
    page1.setLayout(new RowLayout());
    Button button = new Button(page1, SWT.NONE);
    button.setText("Button on page 2");
    button.pack();

    // create the button that will switch between the pages
    Button pageButton = new Button(shell, SWT.PUSH);
    pageButton.setText("Push");
    pageButton.setBounds(clientArea.x + 10, clientArea.y + 10, 80, 25);
    pageButton.addListener(SWT.Selection, event -> {
        pageNum = ++pageNum % 2;
        layout.topControl = pageNum == 0 ? page0 : page1;
        contentPanel.layout();
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 51");
    shell.setBounds(10, 10, 300, 300);// w ww  .j a v  a2  s  . com
    shell.setLayout(new GridLayout(2, true));
    final Table table = new Table(shell, SWT.NONE);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 2;
    table.setLayoutData(data);
    for (int i = 0; i < 99; i++) {
        new TableItem(table, SWT.NONE).setText("item " + i);
    }
    Button upButton = new Button(shell, SWT.PUSH);
    upButton.setText("Scroll up one page");
    upButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    upButton.addListener(SWT.Selection, event -> {
        int height = table.getClientArea().height;
        int visibleItemCount = height / table.getItemHeight();
        int topIndex = table.getTopIndex();
        int newTopIndex = Math.max(0, topIndex - visibleItemCount);
        if (topIndex != newTopIndex) {
            table.setTopIndex(newTopIndex);
        }
    });
    Button downButton = new Button(shell, SWT.PUSH);
    downButton.setText("Scroll down one page");
    downButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    downButton.addListener(SWT.Selection, event -> {
        int height = table.getClientArea().height;
        int visibleItemCount = height / table.getItemHeight();
        int topIndex = table.getTopIndex();
        int newTopIndex = Math.min(table.getItemCount(), topIndex + visibleItemCount);
        if (topIndex != newTopIndex) {
            table.setTopIndex(newTopIndex);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:StackLayoutSwitchComposites.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 300, 200);//from   ww  w . jav a  2 s  . c o  m
    // create the composite that the pages will share
    final Composite contentPanel = new Composite(shell, SWT.BORDER);
    contentPanel.setBounds(100, 10, 190, 90);
    final StackLayout layout = new StackLayout();
    contentPanel.setLayout(layout);

    // create the first page's content
    final Composite page0 = new Composite(contentPanel, SWT.NONE);
    page0.setLayout(new RowLayout());
    Label label = new Label(page0, SWT.NONE);
    label.setText("Label on page 1");
    label.pack();

    // create the second page's content
    final Composite page1 = new Composite(contentPanel, SWT.NONE);
    page1.setLayout(new RowLayout());
    Button button = new Button(page1, SWT.NONE);
    button.setText("Button on page 2");
    button.pack();

    // create the button that will switch between the pages
    Button pageButton = new Button(shell, SWT.PUSH);
    pageButton.setText("Push");
    pageButton.setBounds(10, 10, 80, 25);
    pageButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            pageNum = ++pageNum % 2;
            layout.topControl = pageNum == 0 ? page0 : page1;
            contentPanel.layout();
        }
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 46");
    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setEnabled(false);//from ww w  .  j a  v a 2s  .  c o  m
    composite.setLayout(new FillLayout());
    Button button = new Button(composite, SWT.PUSH);
    button.setText("Button");
    composite.pack();
    composite.setLocation(10, 10);
    final Point[] offset = new Point[1];
    Listener listener = event -> {
        switch (event.type) {
        case SWT.MouseDown:
            Rectangle rect = composite.getBounds();
            if (rect.contains(event.x, event.y)) {
                Point pt1 = composite.toDisplay(0, 0);
                Point pt2 = shell.toDisplay(event.x, event.y);
                offset[0] = new Point(pt2.x - pt1.x, pt2.y - pt1.y);
            }
            break;
        case SWT.MouseMove:
            if (offset[0] != null) {
                Point pt = offset[0];
                composite.setLocation(event.x - pt.x, event.y - pt.y);
            }
            break;
        case SWT.MouseUp:
            offset[0] = null;
            break;
        }
    };
    shell.addListener(SWT.MouseDown, listener);
    shell.addListener(SWT.MouseUp, listener);
    shell.addListener(SWT.MouseMove, listener);
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:FormLayoutComplex.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    FormLayout layout = new FormLayout();
    shell.setLayout(layout);/*from w ww.  ja v a  2 s. c  om*/
    Button one = new Button(shell, SWT.PUSH);
    one.setText("One");
    FormData data = new FormData();
    data.top = new FormAttachment(0, 5);
    data.left = new FormAttachment(0, 5);
    data.bottom = new FormAttachment(50, -5);
    data.right = new FormAttachment(50, -5);
    one.setLayoutData(data);

    Composite composite = new Composite(shell, SWT.NONE);
    GridLayout gridLayout = new GridLayout();
    gridLayout.marginHeight = 20;
    gridLayout.marginWidth = 20;
    composite.setLayout(gridLayout);
    Button two = new Button(composite, SWT.PUSH);
    two.setText("two");
    GridData gridData = new GridData(GridData.FILL_BOTH);
    two.setLayoutData(gridData);
    Button three = new Button(composite, SWT.PUSH);
    three.setText("three");
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    three.setLayoutData(gridData);
    Button four = new Button(composite, SWT.PUSH);
    four.setText("four");
    gridData = new GridData(GridData.FILL_BOTH);
    four.setLayoutData(gridData);
    data = new FormData();
    data.top = new FormAttachment(0, 15);
    data.left = new FormAttachment(one, 25);
    data.bottom = new FormAttachment(50, -15);
    data.right = new FormAttachment(100, -25);
    composite.setLayoutData(data);

    Button five = new Button(shell, SWT.PUSH);
    five.setText("five");
    data = new FormData();
    data.top = new FormAttachment(one, 5);
    data.left = new FormAttachment(0, 5);
    data.bottom = new FormAttachment(100, -5);
    data.right = new FormAttachment(100, -5);
    five.setLayoutData(data);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 181");
    shell.setLayout(new RowLayout(SWT.HORIZONTAL));
    final Table table = new Table(shell, SWT.BORDER | SWT.CHECK);
    table.setLayoutData(new RowData(-1, 300));
    table.setHeaderVisible(true);/*from www  .j ava2 s  .  c  om*/
    TableColumn column = new TableColumn(table, SWT.LEFT);
    column.setText("Column 0");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 1");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 2");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 3");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 4");
    for (int i = 0; i < 100; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        String[] text = new String[] { i + " 0", i + " 1", i + " 2", i + " 3", i + " 4" };
        item.setText(text);
    }
    Listener listener = e -> System.out.println("Move " + e.widget);
    TableColumn[] columns = table.getColumns();
    for (int i = 0; i < columns.length; i++) {
        columns[i].pack();
        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 = table.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;
        }
        table.setColumnOrder(order);
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:BackgroundImageControl.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
    FillLayout layout1 = new FillLayout(SWT.VERTICAL);
    layout1.marginWidth = layout1.marginHeight = 10;
    shell.setLayout(layout1);//from w w w  .  j a  v a 2 s.c o  m
    Group group = new Group(shell, SWT.NONE);
    group.setText("Group ");
    RowLayout layout2 = new RowLayout(SWT.VERTICAL);
    layout2.marginWidth = layout2.marginHeight = layout2.spacing = 10;
    group.setLayout(layout2);
    for (int i = 0; i < 8; i++) {
        Button button = new Button(group, SWT.RADIO);
        button.setText("Button " + i);
    }
    shell.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle rect = shell.getClientArea();
            Image newImage = new Image(display, Math.max(1, rect.width), 1);
            GC gc = new GC(newImage);
            gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
            gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
            gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
            gc.dispose();
            shell.setBackgroundImage(newImage);
            if (oldImage != null)
                oldImage.dispose();
            oldImage = newImage;
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (oldImage != null)
        oldImage.dispose();
    display.dispose();
}

From source file:Snippet181.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.HORIZONTAL));
    final Table table = new Table(shell, SWT.BORDER | SWT.CHECK);
    table.setLayoutData(new RowData(-1, 300));
    table.setHeaderVisible(true);// w w w  . j av a2  s.c o m
    TableColumn column = new TableColumn(table, SWT.LEFT);
    column.setText("Column 0");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 1");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 2");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 3");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 4");
    for (int i = 0; i < 100; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        String[] text = new String[] { i + " 0", i + " 1", i + " 2", i + " 3", i + " 4" };
        item.setText(text);
    }
    Listener listener = new Listener() {
        public void handleEvent(Event e) {
            System.out.println("Move " + e.widget);
        }
    };
    TableColumn[] columns = table.getColumns();
    for (int i = 0; i < columns.length; i++) {
        columns[i].pack();
        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, new Listener() {
        public void handleEvent(Event e) {
            int[] order = table.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;
            }
            table.setColumnOrder(order);
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}