Example usage for org.eclipse.swt.widgets Button Button

List of usage examples for org.eclipse.swt.widgets Button Button

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button Button.

Prototype


public Button(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

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

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

    Link link = new Link(shell, SWT.BORDER);
    link.setText("This a very simple <a>link</a> widget.");

    Button setButton = new Button(shell, SWT.PUSH);
    setButton.setText("Choose link color");
    setButton.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("default link color " + link.getLinkForeground());
        ColorDialog colorDialog = new ColorDialog(shell);
        RGB color = colorDialog.open();//from   w ww.  j a  v  a  2  s.co  m
        link.setLinkForeground(new Color(display, color));
        System.out.println("user selected link color " + link.getLinkForeground());
    }));

    Button resetButton = new Button(shell, SWT.PUSH);
    resetButton.setText("Reset link color");
    resetButton.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("link color reset to system default");
        link.setLinkForeground(null);
    }));

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 167");
    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  www. java 2 s  .  co 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 = widgetSelectedAdapter(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 = widgetSelectedAdapter(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:org.eclipse.swt.snippets.Snippet253.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 253");
    FillLayout layout = new FillLayout(SWT.VERTICAL);
    shell.setLayout(layout);//from  w w w  . java  2  s  .  c o  m
    final Table table = new Table(shell, SWT.NONE);
    for (int i = 0; i < 32; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText("Item " + (i + 1) + " is quite long");
    }
    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Visible Items []");
    button.addListener(SWT.Selection, e -> {
        Rectangle rect = table.getClientArea();
        int itemHeight = table.getItemHeight();
        int headerHeight = table.getHeaderHeight();
        int visibleCount = (rect.height - headerHeight + itemHeight - 1) / itemHeight;
        button.setText("Visible Items [" + visibleCount + "]");
    });
    shell.setSize(200, 250);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 188");
    shell.setLayout(new GridLayout());
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    Composite c = new Composite(sc, SWT.NONE);
    c.setLayout(new GridLayout(10, true));
    for (int i = 0; i < 300; i++) {
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button " + i);
    }//from w  w  w  .j  a v  a2 s  . c om
    sc.setContent(c);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    sc.setShowFocusedControl(true);

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

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

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

    Composite c1 = new Composite(shell, SWT.BORDER);
    c1.setLayout(new RowLayout());
    Button b1 = new Button(c1, SWT.PUSH);
    b1.setText("B&1");
    Button r1 = new Button(c1, SWT.RADIO);
    r1.setText("R1");
    Button r2 = new Button(c1, SWT.RADIO);
    r2.setText("R&2");
    Button r3 = new Button(c1, SWT.RADIO);
    r3.setText("R3");
    Button b2 = new Button(c1, SWT.PUSH);
    b2.setText("B2");
    List l1 = new List(c1, SWT.SINGLE | SWT.BORDER);
    l1.setItems("L1");
    Button b3 = new Button(c1, SWT.PUSH);
    b3.setText("B&3");
    Button b4 = new Button(c1, SWT.PUSH);
    b4.setText("B&4");

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

    List l2 = new List(shell, SWT.SINGLE | SWT.BORDER);
    l2.setItems("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("I2");
    Combo combo1 = new Combo(tb1, SWT.READ_ONLY | SWT.BORDER);
    combo1.setItems("C1");
    combo1.setText("C1");
    combo1.pack();//  w w w .j a v  a  2  s  . com
    ToolItem i3 = new ToolItem(tb1, SWT.SEPARATOR);
    i3.setWidth(combo1.getSize().x);
    i3.setControl(combo1);
    ToolItem i4 = new ToolItem(tb1, SWT.PUSH);
    i4.setText("I&4");
    ToolItem i5 = new ToolItem(tb1, SWT.CHECK);
    i5.setText("I5");

    Button b7 = new Button(shell, SWT.PUSH);
    b7.setText("B&7");

    Composite c4 = new Composite(shell, SWT.BORDER);
    Composite c5 = new Composite(c4, SWT.BORDER);
    c5.setLayout(new FillLayout());
    new Label(c5, SWT.NONE).setText("No");
    c5.pack();

    Control[] tabList1 = new Control[] { b2, b1, b3 };
    c1.setTabList(tabList1);
    Control[] tabList2 = new Control[] { c1, b7, tb1, c4, c2, l2 };
    shell.setTabList(tabList2);

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

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

From source file:ScollCompositeControl.java

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

    // this button is always 400 x 400. Scrollbars appear if the window is
    // resized to be
    // too small to show part of the button
    ScrolledComposite c1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button b1 = new Button(c1, SWT.PUSH);
    b1.setText("fixed size button");
    b1.setSize(400, 400);/*from www .  ja v a 2  s. c o  m*/
    c1.setContent(b1);

    shell.setSize(600, 300);
    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 w  w .jav a 2  s  .c  o m*/
    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:CompositeCreateDisposeChildren.java

public static void main(String args[]) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Push");
    pageComposite = new Composite(shell, SWT.NONE);
    pageComposite.setLayout(new GridLayout());
    pageComposite.setLayoutData(new GridData());

    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if ((pageComposite != null) && (!pageComposite.isDisposed())) {
                pageComposite.dispose();
            }/*w  ww. jav a2 s  . com*/
            pageComposite = new Composite(shell, SWT.NONE);
            pageComposite.setLayout(new GridLayout());
            pageComposite.setLayoutData(new GridData());
            if (pageNum++ % 2 == 0) {
                Table table = new Table(pageComposite, SWT.BORDER);
                table.setLayoutData(new GridData());
                for (int i = 0; i < 5; i++) {
                    new TableItem(table, SWT.NONE).setText("table item " + i);
                }
            } else {
                new Button(pageComposite, SWT.RADIO).setText("radio");
            }
            shell.layout(true);
        }
    });

    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);/*  ww w  .  j  a  v a  2s  . 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:TimeStopWhenButtonPressing.java

public static void main(String[] args) {
    final Display display = new Display();
    final Color red = display.getSystemColor(SWT.COLOR_RED);
    final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Stop Timer");
    final Label label = new Label(shell, SWT.BORDER);
    label.setBackground(red);//  w  ww  .j  a  v a2 s. co  m
    final int time = 500;
    final Runnable timer = new Runnable() {
        public void run() {
            if (label.isDisposed())
                return;
            Color color = label.getBackground().equals(red) ? blue : red;
            label.setBackground(color);
            display.timerExec(time, this);
        }
    };
    display.timerExec(time, timer);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            display.timerExec(-1, timer);
        }
    });
    button.pack();
    label.setLayoutData(new RowData(button.getSize()));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}