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

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

    shell.setLayout(new BorderLayout());

    Button buttonWest = new Button(shell, SWT.PUSH);
    buttonWest.setText("West");
    buttonWest.setLayoutData(new BorderLayout.BorderData(BorderLayout.WEST));

    Button buttonEast = new Button(shell, SWT.PUSH);
    buttonEast.setText("East");
    buttonEast.setLayoutData(new BorderLayout.BorderData(BorderLayout.EAST));

    Button buttonNorth = new Button(shell, SWT.PUSH);
    buttonNorth.setText("North");
    buttonNorth.setLayoutData(new BorderLayout.BorderData(BorderLayout.NORTH));

    Button buttonSouth = new Button(shell, SWT.PUSH);
    buttonSouth.setText("South");
    buttonSouth.setLayoutData(new BorderLayout.BorderData(BorderLayout.SOUTH));

    Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    text.setText("Center");
    text.setLayoutData(new BorderLayout.BorderData(BorderLayout.CENTER));

    shell.pack();/*from  w  w w .  ja v  a2s. c  o  m*/
    shell.open();
    // textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();

}

From source file:StackLayoutControlMargin.java

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

    final StackLayout stackLayout = new StackLayout();
    stackLayout.marginHeight = 20;/*from  ww w  . j a  v  a2 s  .c  o m*/
    stackLayout.marginWidth = 20;
    shell.setLayout(stackLayout);

    final Button[] buttons = new Button[3];

    for (int i = 0; i < buttons.length; i++) {
        buttons[i] = new Button(shell, SWT.NULL);
        buttons[i].setText("Button #" + i);

        buttons[i].addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent e) {
                // Flip to next button.
                Button nextButton = null;
                for (int i = 0; i < buttons.length; i++) {
                    if (buttons[i] == e.widget) {
                        if (i == buttons.length - 1)
                            nextButton = buttons[0];
                        else
                            nextButton = buttons[i + 1];
                    }
                }
                stackLayout.topControl = nextButton;
                shell.layout();
            }

            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });
    }

    stackLayout.topControl = buttons[0];

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

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Cursor cursor = display.getSystemCursor(SWT.CURSOR_HAND);
    Shell shell = new Shell(display);
    shell.setText("Snippet 44");
    shell.open();//from  w w  w . java 2 s . c o  m
    final Button b = new Button(shell, 0);
    b.setText("Push to set cursor to hand");
    Rectangle clientArea = shell.getClientArea();
    b.setBounds(clientArea.x + 10, clientArea.y + 10, 200, 200);
    b.addListener(SWT.Selection, e -> b.setCursor(cursor));
    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);/*ww w  . j  a v a2s  . 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.Snippet175.java

public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 175");
    shell.setLayout(new GridLayout(3, false));

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

    final Button bHidden = new Button(shell, SWT.PUSH);
    bHidden.setText("Button 1");
    GridData data = new GridData();
    data.exclude = true;/*from  w ww  .java 2  s .c  o m*/
    data.horizontalSpan = 2;
    data.horizontalAlignment = SWT.FILL;
    bHidden.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 2");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 4");

    b = new Button(shell, SWT.CHECK);
    b.setText("hide");
    b.setSelection(true);
    b.addListener(SWT.Selection, e -> {
        Button b1 = (Button) e.widget;
        GridData data1 = (GridData) bHidden.getLayoutData();
        data1.exclude = b1.getSelection();
        bHidden.setVisible(!data1.exclude);
        shell.layout(false);
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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 av  a 2s. 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:ControlListenerAdd.java

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

    shell.setLayout(new RowLayout());

    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new RowLayout());
    composite.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
    composite.addControlListener(new ControlListener() {
        public void controlMoved(ControlEvent e) {
        }//from  w  ww  .  j a  va2s.com

        public void controlResized(ControlEvent e) {
            System.out.println("Composite resize.");
        }
    });

    Button buttonAdd = new Button(shell, SWT.PUSH);
    buttonAdd.setText("Add new button");
    buttonAdd.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Button button = new Button(composite, SWT.PUSH);
            button.setText("Button #" + (count++));
            composite.layout(true);
            composite.pack();
        }
    });

    shell.setSize(450, 100);
    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;//from  w  ww . j  a  v a 2 s.c  om
    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: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 w w w.  j  a  v a  2s.c om*/

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

public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);

    GridLayout gl = new GridLayout();
    gl.numColumns = 4;//w ww  .j a va2 s .  c  o  m
    s.setLayout(gl);
    s.setSize(250, 275);

    s.setText("A Shell Composite Example");

    s.setLayout(gl);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 4;
    gd = new GridData();

    Composite c1 = new Composite(s, SWT.NO_FOCUS);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    c1.setLayoutData(gd);
    Composite c2 = new Composite(s, SWT.NO_FOCUS);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    c2.setLayoutData(gd);

    Composite c = new Composite(s, SWT.NO_FOCUS);
    c.setLayout(new RowLayout());
    Button b1 = new Button(c, SWT.PUSH | SWT.BORDER);
    b1.setText("OK");
    Button b2 = new Button(c, SWT.PUSH | SWT.BORDER);
    b2.setText("Cancel");
    gd = new GridData(GridData.FILL_HORIZONTAL);
    c.setLayoutData(gd);

    s.open();
    while (!s.isDisposed()) {
        if (!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
}