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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 248");
    shell.setLayout(new FillLayout());
    shell.addListener(SWT.Traverse, event -> {
        switch (event.detail) {
        case SWT.TRAVERSE_ESCAPE:
            shell.close();/*w w w  .  ja v a2 s . c  o m*/
            event.detail = SWT.TRAVERSE_NONE;
            event.doit = false;
            break;
        }
    });
    Button button = new Button(shell, SWT.PUSH);
    button.setText("A Button (that doesn't process Escape)");
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ScreenshotCaptureGC.java

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

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.pack();//from w w  w.j a  v  a  2 s .c  om
    button.setLocation(10, 140);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            GC gc = new GC(display);
            final Image image = new Image(display, 400, 400);
            gc.copyArea(image, 0, 0);
            gc.dispose();

            Shell popup = new Shell(shell);
            popup.setText("Image");
            popup.addListener(SWT.Close, new Listener() {
                public void handleEvent(Event e) {
                    image.dispose();
                }
            });

            Canvas canvas = new Canvas(popup, SWT.NONE);
            canvas.setBounds(10, 10, 400 + 10, 400 + 10);
            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.pack();
            popup.open();
        }
    });
    shell.pack();
    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());
        }//from  w ww  .  j a  v a2  s. c om
    });
    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.Snippet76.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 76");
    final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
    Rectangle clientArea = shell.getClientArea();
    tabFolder.setLocation(clientArea.x, clientArea.y);
    for (int i = 0; i < 6; i++) {
        TabItem item = new TabItem(tabFolder, SWT.NONE);
        item.setText("TabItem " + i);
        Button button = new Button(tabFolder, SWT.PUSH);
        button.setText("Page " + i);
        item.setControl(button);//from   w w w .  j a va2s  .  c o  m
    }
    tabFolder.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 5");
    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   ww  w. j av  a 2 s. c o  m*/
    c1.setContent(b1);

    // this button has a minimum size of 400 x 400. If the window is resized to be big
    // enough to show more than 400 x 400, the button will grow in size. If the window
    // is made too small to show 400 x 400, scrollbars will appear.
    ScrolledComposite c2 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button b2 = new Button(c2, SWT.PUSH);
    b2.setText("expanding button");
    c2.setContent(b2);
    c2.setExpandHorizontal(true);
    c2.setExpandVertical(true);
    c2.setMinSize(400, 400);

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

From source file:DialogShellPromptValue.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.pack();/*from  ww  w .  j a va 2 s. c  o m*/

    final boolean[] result = new boolean[1];
    final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
    dialog.setLayout(new RowLayout());
    final Button ok = new Button(dialog, SWT.PUSH);
    ok.setText("OK");
    Button cancel = new Button(dialog, SWT.PUSH);
    cancel.setText("Cancel");
    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            result[0] = event.widget == ok;
            dialog.close();
        }
    };
    ok.addListener(SWT.Selection, listener);
    cancel.addListener(SWT.Selection, listener);
    dialog.pack();
    dialog.open();
    System.out.println("Prompt ...");
    while (!dialog.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    System.out.println("Result: " + result[0]);
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Clipboard clipboard = new Clipboard(display);
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setText("Snippet 282");
    shell.setLayout(new GridLayout());
    shell.setText("Clipboard ImageTransfer");

    final Button imageButton = new Button(shell, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.minimumHeight = 400;//  ww w.  j  a va 2s.co  m
    gd.minimumWidth = 600;
    imageButton.setLayoutData(gd);

    final Text imageText = new Text(shell, SWT.BORDER);
    imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    Composite buttons = new Composite(shell, SWT.NONE);
    buttons.setLayout(new GridLayout(4, true));
    Button button = new Button(buttons, SWT.PUSH);
    button.setText("Open");
    button.addListener(SWT.Selection, event -> {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setText("Open an image file or cancel");
        String string = dialog.open();
        if (string != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null)
                image.dispose();
            image = new Image(display, string);
            imageButton.setImage(image);
            imageText.setText(string);
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Copy");
    button.addListener(SWT.Selection, event -> {
        Image image = imageButton.getImage();
        if (image != null) {
            ImageTransfer imageTransfer = ImageTransfer.getInstance();
            TextTransfer textTransfer = TextTransfer.getInstance();
            clipboard.setContents(new Object[] { image.getImageData(), imageText.getText() },
                    new Transfer[] { imageTransfer, textTransfer });
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Paste");
    button.addListener(SWT.Selection, event -> {
        ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
        if (imageData != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null)
                image.dispose();
            image = new Image(display, imageData);
            imageButton.setImage(image);
        } else {
            imageButton.setText("No image");
            imageButton.setImage(null);
        }
        String text = (String) clipboard.getContents(TextTransfer.getInstance());
        if (text != null) {
            imageText.setText(text);
        } else {
            imageText.setText("");
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Clear");
    button.addListener(SWT.Selection, event -> {
        imageButton.setText("");
        Image image = imageButton.getImage();
        if (image != null)
            image.dispose();
        imageButton.setImage(null);
        imageText.setText("");
    });

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

From source file:DialogOKCancelFormLayout.java

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

    Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
    Label label = new Label(dialog, SWT.NONE);
    label.setText("Exit the application?");
    Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setText("&OK");
    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setText("&Cancel");

    FormLayout form = new FormLayout();
    form.marginWidth = form.marginHeight = 8;
    dialog.setLayout(form);//from   www. j  a va 2s .  c o m
    FormData okData = new FormData();
    okData.top = new FormAttachment(label, 8);
    okButton.setLayoutData(okData);
    FormData cancelData = new FormData();
    cancelData.left = new FormAttachment(okButton, 8);
    cancelData.top = new FormAttachment(okButton, 0, SWT.TOP);
    cancelButton.setLayoutData(cancelData);

    dialog.setDefaultButton(okButton);
    dialog.pack();
    dialog.open();

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

From source file:TableEditorSample.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Text Table Editor");

    shell.setLayout(new FillLayout());

    Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
    table.setHeaderVisible(true);//  w  ww  .j a  v  a2  s .  c  o  m
    table.setLinesVisible(true);

    for (int i = 0; i < 5; i++) {
        TableColumn column = new TableColumn(table, SWT.CENTER);
        column.setText("Column " + (i + 1));
        column.pack();
    }
    // Create five table editors for color
    TableEditor[] colorEditors = new TableEditor[5];

    // Create five buttons for changing color
    Button[] colorButtons = new Button[5];

    for (int i = 0; i < 5; i++) {
        final TableItem item = new TableItem(table, SWT.NONE);
        // Create the editor and button
        colorEditors[i] = new TableEditor(table);
        colorButtons[i] = new Button(table, SWT.PUSH);

        // Set attributes of the button
        colorButtons[i].setText("Color...");
        colorButtons[i].computeSize(SWT.DEFAULT, table.getItemHeight());

        // Set attributes of the editor
        colorEditors[i].grabHorizontal = true;
        colorEditors[i].minimumHeight = colorButtons[i].getSize().y;
        colorEditors[i].minimumWidth = colorButtons[i].getSize().x;

        // Set the editor for the first column in the row
        colorEditors[i].setEditor(colorButtons[i], item, 0);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();

}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 65");
    Label label = new Label(shell, SWT.WRAP);
    label.setText("This is a long text string that will wrap when the dialog is resized.");
    List list = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    list.setItems("Item 1", "Item 2");
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("OK");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Cancel");

    final int insetX = 4, insetY = 4;
    FormLayout formLayout = new FormLayout();
    formLayout.marginWidth = insetX;/*from  www  . ja  va2s  .  c  om*/
    formLayout.marginHeight = insetY;
    shell.setLayout(formLayout);

    Point size = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    final FormData labelData = new FormData(size.x, SWT.DEFAULT);
    labelData.left = new FormAttachment(0, 0);
    labelData.right = new FormAttachment(100, 0);
    label.setLayoutData(labelData);
    shell.addListener(SWT.Resize, e -> {
        Rectangle rect = shell.getClientArea();
        labelData.width = rect.width - insetX * 2;
        shell.layout();
    });

    FormData button2Data = new FormData();
    button2Data.right = new FormAttachment(100, -insetX);
    button2Data.bottom = new FormAttachment(100, 0);
    button2.setLayoutData(button2Data);

    FormData button1Data = new FormData();
    button1Data.right = new FormAttachment(button2, -insetX);
    button1Data.bottom = new FormAttachment(100, 0);
    button1.setLayoutData(button1Data);

    FormData listData = new FormData();
    listData.left = new FormAttachment(0, 0);
    listData.right = new FormAttachment(100, 0);
    listData.top = new FormAttachment(label, insetY);
    listData.bottom = new FormAttachment(button2, -insetY);
    list.setLayoutData(listData);

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