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

TableShellExample3() {
    d = new Display();
    s = new Shell(d);

    s.setSize(250, 200);//from w ww  .  j a va 2s.c  o  m

    s.setText("A Table Shell Example");
    GridLayout gl = new GridLayout();
    gl.numColumns = 4;
    s.setLayout(gl);

    final Table t = new Table(s, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION);
    final GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 4;
    t.setLayoutData(gd);
    t.setHeaderVisible(true);
    final TableColumn tc1 = new TableColumn(t, SWT.LEFT);
    final TableColumn tc2 = new TableColumn(t, SWT.CENTER);
    final TableColumn tc3 = new TableColumn(t, SWT.CENTER);
    tc1.setText("First Name");
    tc2.setText("Last Name");
    tc3.setText("Address");
    tc1.setWidth(70);
    tc2.setWidth(70);
    tc3.setWidth(80);
    final TableItem item1 = new TableItem(t, SWT.NONE);
    item1.setText(new String[] { "Tim", "Hatton", "Kentucky" });
    final TableItem item2 = new TableItem(t, SWT.NONE);
    item2.setText(new String[] { "Caitlyn", "Warner", "Ohio" });
    final TableItem item3 = new TableItem(t, SWT.NONE);
    item3.setText(new String[] { "Reese", "Miller", "Ohio" });

    final Text find = new Text(s, SWT.SINGLE | SWT.BORDER);
    final Text replace = new Text(s, SWT.SINGLE | SWT.BORDER);
    final Button replaceBtn = new Button(s, SWT.BORDER | SWT.PUSH);
    replaceBtn.setText("Replace");
    replaceBtn.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TableItem[] tia = t.getItems();

            for (int i = 0; i < tia.length; i++) {
                if (tia[i].getText(2).equals(find.getText())) {
                    tia[i].setText(2, replace.getText());
                }

            }
        }
    });

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

From source file:SendMessage.java

/**
 * Creates the main window's contents//from ww w . ja  va2 s  .  c  om
 * 
 * @param parent the main window
 * @return Control
 */
protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(5, true));

    // Create a big text box for the message text
    final Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 5;
    text.setLayoutData(data);

    // Create the Confirm button
    Button confirm = new Button(composite, SWT.PUSH);
    confirm.setText("Confirm");
    confirm.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // Create the Error button
    Button error = new Button(composite, SWT.PUSH);
    error.setText("Error");
    error.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // Create the Information button
    Button information = new Button(composite, SWT.PUSH);
    information.setText("Information");
    information.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // Create the Question button
    Button question = new Button(composite, SWT.PUSH);
    question.setText("Question");
    question.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // Create the Warning button
    Button warning = new Button(composite, SWT.PUSH);
    warning.setText("Warning");
    warning.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // Create the label to display the return value
    final Label label = new Label(composite, SWT.NONE);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 5;
    label.setLayoutData(data);

    // Save ourselves some typing
    final Shell shell = parent.getShell();

    // Display a Confirmation dialog
    confirm.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            boolean b = MessageDialog.openConfirm(shell, "Confirm", text.getText());
            label.setText("Returned " + Boolean.toString(b));
        }
    });

    // Display an Error dialog
    error.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            MessageDialog.openError(shell, "Error", text.getText());
            label.setText("Returned void");
        }
    });

    // Display an Information dialog
    information.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            MessageDialog.openInformation(shell, "Information", text.getText());
            label.setText("Returned void");
        }
    });

    // Display a Question dialog
    question.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            boolean b = MessageDialog.openQuestion(shell, "Question", text.getText());
            label.setText("Returned " + Boolean.toString(b));
        }
    });

    // Display a Warning dialog
    warning.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            MessageDialog.openWarning(shell, "Warning", text.getText());
            label.setText("Returned void");
        }
    });

    return composite;
}

From source file:DialogExamples.java

protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NULL);

    composite.setLayout(new GridLayout());

    /* ------ MessageDialog ------------- */
    // openQuestion
    final Button buttonOpenMessage = new Button(composite, SWT.PUSH);
    buttonOpenMessage.setText("Demo: MessageDialog.openQuestion");
    buttonOpenMessage.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {

            boolean answer = MessageDialog.openQuestion(getShell(), "A Simple Question",
                    "Is SWT/JFace your favorite Java UI framework?");
            System.out.println("Your answer is " + (answer ? "YES" : "NO"));
        }//from w w  w  .  j  a va 2 s.  c  o  m
    });

    final Button buttonMessageDialog = new Button(composite, SWT.PUSH);
    buttonMessageDialog.setText("Demo: new MessageDialog");
    buttonMessageDialog.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            MessageDialog dialog = new MessageDialog(getShell(), "Select your favorite Java UI framework", null,
                    "Which one of the following is your favorite Java UI framework?", MessageDialog.QUESTION,
                    new String[] { "AWT", "Swing", "SWT/JFace" }, 2);
            int answer = dialog.open();

            switch (answer) {
            case -1: // if the user closes the dialog without clicking any button.
                System.out.println("No selection");
                break;

            case 0:
                System.out.println("Your selection is: AWT");
                break;
            case 1:
                System.out.println("Your selection is: Swing");
                break;
            case 2:
                System.out.println("Your selection is: SWT/JFace");
                break;

            }
        }
    });

    /* ------ InputDialog ------------- */
    final Button buttonInputDialog = new Button(composite, SWT.PUSH);
    buttonInputDialog.setText("Demo: InputDialog");
    buttonInputDialog.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            IInputValidator validator = new IInputValidator() {
                public String isValid(String newText) {
                    if (newText.equalsIgnoreCase("SWT/JFace") || newText.equalsIgnoreCase("AWT")
                            || newText.equalsIgnoreCase("Swing"))
                        return null;
                    else
                        return "The allowed values are: SWT/JFace, AWT, Swing";
                }
            };
            InputDialog dialog = new InputDialog(getShell(), "Question",
                    "What's your favorite Java UI framework?", "SWT/JFace", validator);
            if (dialog.open() == Window.OK) {
                System.out.println("Your favorite Java UI framework is: " + dialog.getValue());
            } else {
                System.out.println("Action cancelled");
            }
        }
    });

    /* ------ ProgressMonitorDialog ------------- */
    final Button buttonProgressDialog = new Button(composite, SWT.PUSH);
    buttonProgressDialog.setText("Demo: ProgressMonitorDialog");
    buttonProgressDialog.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor)
                        throws InvocationTargetException, InterruptedException {
                    monitor.beginTask("Number counting", 10);
                    for (int i = 0; i < 10; i++) {
                        if (monitor.isCanceled()) {
                            monitor.done();
                            return;
                        }

                        System.out.println("Count number: " + i);
                        monitor.worked(1);
                        Thread.sleep(500); // 0.5s.
                    }
                    monitor.done();
                }
            };

            ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
            try {
                dialog.run(true, true, runnableWithProgress);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    });
    return super.createContents(parent);
}

From source file:org.eclipse.swt.examples.controlexample.AlignableTab.java

/**
 * Creates the "Other" group./*from  w ww.  j  a v a 2  s . c o m*/
 */
@Override
void createOtherGroup() {
    super.createOtherGroup();

    /* Create the group */
    alignmentGroup = new Group(otherGroup, SWT.NONE);
    alignmentGroup.setLayout(new GridLayout());
    alignmentGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    alignmentGroup.setText(ControlExample.getResourceString("Alignment"));

    /* Create the controls */
    leftButton = new Button(alignmentGroup, SWT.RADIO);
    leftButton.setText(ControlExample.getResourceString("Left"));
    centerButton = new Button(alignmentGroup, SWT.RADIO);
    centerButton.setText(ControlExample.getResourceString("Center"));
    rightButton = new Button(alignmentGroup, SWT.RADIO);
    rightButton.setText(ControlExample.getResourceString("Right"));

    /* Add the listeners */
    SelectionListener selectionListener = widgetSelectedAdapter(event -> {
        if (!((Button) event.widget).getSelection())
            return;
        setExampleWidgetAlignment();
    });
    leftButton.addSelectionListener(selectionListener);
    centerButton.addSelectionListener(selectionListener);
    rightButton.addSelectionListener(selectionListener);
}

From source file:TabbedShellExample.java

TabbedShellExample() {
    d = new Display();
    s = new Shell(d);

    s.setSize(250, 200);/*from  w  w w.jav a  2s  . c o  m*/

    s.setText("A Tabbed Shell Example");
    s.setLayout(new FillLayout());

    TabFolder tf = new TabFolder(s, SWT.BORDER);

    TabItem ti1 = new TabItem(tf, SWT.BORDER);
    ti1.setText("Option Group");
    ti1.setControl(new GroupExample(tf, SWT.SHADOW_ETCHED_IN));

    TabItem ti2 = new TabItem(tf, SWT.BORDER);
    ti2.setText("Grid");
    ti2.setControl(new GridComposite(tf));

    TabItem ti3 = new TabItem(tf, SWT.BORDER);
    ti3.setText("Text");
    Composite c1 = new Composite(tf, SWT.BORDER);
    c1.setLayout(new FillLayout());
    Text t = new Text(c1, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
    ti3.setControl(c1);

    TabItem ti4 = new TabItem(tf, SWT.BORDER);
    ti4.setText("Settings");
    Composite c2 = new Composite(tf, SWT.BORDER);
    c2.setLayout(new RowLayout());
    Text t2 = new Text(c2, SWT.BORDER | SWT.SINGLE | SWT.WRAP | SWT.V_SCROLL);
    Button b = new Button(c2, SWT.PUSH | SWT.BORDER);
    b.setText("Save");
    ti4.setControl(c2);

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

From source file:MainClass.java

public void createContents(Shell shell, String location) {
    shell.setLayout(new FormLayout());

    Composite controls = new Composite(shell, SWT.NONE);
    FormData data = new FormData();
    data.top = new FormAttachment(0, 0);
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    controls.setLayoutData(data);/*from w  w  w  . j a  v  a  2 s . c o  m*/

    Label status = new Label(shell, SWT.NONE);
    data = new FormData();
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    data.bottom = new FormAttachment(100, 0);
    status.setLayoutData(data);

    final Browser browser = new Browser(shell, SWT.BORDER);
    data = new FormData();
    data.top = new FormAttachment(controls);
    data.bottom = new FormAttachment(status);
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    browser.setLayoutData(data);

    controls.setLayout(new GridLayout(7, false));

    Button button = new Button(controls, SWT.PUSH);
    button.setText("Back");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.back();
        }
    });

    button = new Button(controls, SWT.PUSH);
    button.setText("Forward");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.forward();
        }
    });

    button = new Button(controls, SWT.PUSH);
    button.setText("Refresh");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.refresh();
        }
    });

    button = new Button(controls, SWT.PUSH);
    button.setText("Stop");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.stop();
        }
    });

    final Text url = new Text(controls, SWT.BORDER);
    url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    url.setFocus();

    button = new Button(controls, SWT.PUSH);
    button.setText("Go");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.setUrl(url.getText());
        }
    });

    Label throbber = new Label(controls, SWT.NONE);
    throbber.setText(AT_REST);

    shell.setDefaultButton(button);

    browser.addCloseWindowListener(new AdvancedCloseWindowListener());
    browser.addLocationListener(new AdvancedLocationListener(url));
    browser.addProgressListener(new AdvancedProgressListener(throbber));
    browser.addStatusTextListener(new AdvancedStatusTextListener(status));

    if (location != null) {
        browser.setUrl(location);
    }
}

From source file:FocusTraversal.java

private void init() {
    shell.setLayout(new RowLayout());

    Composite composite1 = new Composite(shell, SWT.BORDER);
    composite1.setLayout(new RowLayout());
    composite1.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

    Button button1 = new Button(composite1, SWT.PUSH);
    button1.setText("Button1");

    List list = new List(composite1, SWT.MULTI | SWT.BORDER);
    list.setItems(new String[] { "Item-1", "Item-2", "Item-3" });

    Button radioButton1 = new Button(composite1, SWT.RADIO);
    radioButton1.setText("radio-1");
    Button radioButton2 = new Button(composite1, SWT.RADIO);
    radioButton2.setText("radio-2");

    Composite composite2 = new Composite(shell, SWT.BORDER);
    composite2.setLayout(new RowLayout());
    composite2.setBackground(display.getSystemColor(SWT.COLOR_GREEN));

    Button button2 = new Button(composite2, SWT.PUSH);
    button2.setText("Button2");

    final Canvas canvas = new Canvas(composite2, SWT.NULL);
    canvas.setSize(50, 50);/*w  ww  .  ja  va 2  s  .  c o m*/
    canvas.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));

    Combo combo = new Combo(composite2, SWT.DROP_DOWN);
    combo.add("combo");
    combo.select(0);

    canvas.addKeyListener(new KeyListener() {
        public void keyPressed(KeyEvent e) {
            GC gc = new GC(canvas);
            // Erase background first. 
            Rectangle rect = canvas.getClientArea();
            gc.fillRectangle(rect.x, rect.y, rect.width, rect.height);

            Font font = new Font(display, "Arial", 32, SWT.BOLD);
            gc.setFont(font);

            gc.drawString("" + e.character, 15, 10);

            gc.dispose();
            font.dispose();
        }

        public void keyReleased(KeyEvent e) {
        }
    });

    canvas.addTraverseListener(new TraverseListener() {
        public void keyTraversed(TraverseEvent e) {
            if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS)
                e.doit = true;
        }
    });

    composite1.setTabList(new Control[] { button1, list });
    composite2.setTabList(new Control[] { button2, canvas, combo });

    shell.setTabList(new Control[] { composite2, composite1 });
}

From source file:Ch12FileBrowserComposite.java

public Ch12FileBrowserComposite(Composite parent) {
    super(parent, SWT.NONE);

    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    setLayout(layout);/*from ww  w.j av  a2s.c om*/

    Button copyButton = new Button(this, SWT.PUSH);
    copyButton.setText("Copy");
    copyButton.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            Clipboard clipboard = new Clipboard(getDisplay());

            FileTransfer transfer = FileTransfer.getInstance();
            clipboard.setContents(new Object[] { browser.getSelectedFiles() }, new Transfer[] { transfer });
            clipboard.dispose();
        }

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

    Button pasteButton = new Button(this, SWT.PUSH);
    pasteButton.setText("Paste");
    pasteButton.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            Clipboard clipboard = new Clipboard(getDisplay());
            FileTransfer transfer = FileTransfer.getInstance();

            Object data = clipboard.getContents(transfer);
            if (data != null) {
                browser.copyFiles((String[]) data);
            }
            clipboard.dispose();
        }

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

    browser = new FileBrowser(this);
    new FileBrowser(this);
}

From source file:FontRegistryExample.java

private void init() {
    shell.setLayout(new GridLayout(2, false));

    fontRegistry = new FontRegistry(display);

    fontRegistry.put("button-text", new FontData[] { new FontData("Arial", 9, SWT.BOLD) });
    fontRegistry.put("code", new FontData[] { new FontData("Courier New", 10, SWT.NORMAL) });

    Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP);
    text.setFont(fontRegistry.get("code"));
    text.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
    text.setText("public static void main() {\n\tSystem.out.println(\"Hello\"); \n}");
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 2;/*  w  w  w. j a  v  a 2  s  . c  o  m*/
    text.setLayoutData(gd);

    Button executeButton = new Button(shell, SWT.PUSH);
    executeButton.setText("Execute");
    executeButton.setFont(fontRegistry.get("button-text"));

    Button cancelButton = new Button(shell, SWT.PUSH);
    cancelButton.setText("Cancel");
    cancelButton.setFont(fontRegistry.get("button-text"));

}

From source file:SimpleBrowser.java

/**
 * Creates the main window's contents/* w  w  w. j  ava2s.  c om*/
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FormLayout());

    // Create the composite to hold the buttons and text field
    Composite controls = new Composite(shell, SWT.NONE);
    FormData data = new FormData();
    data.top = new FormAttachment(0, 0);
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    controls.setLayoutData(data);

    // Create the web browser
    final Browser browser = new Browser(shell, SWT.NONE);
    data = new FormData();
    data.top = new FormAttachment(controls);
    data.bottom = new FormAttachment(100, 0);
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    browser.setLayoutData(data);

    // Create the controls and wire them to the browser
    controls.setLayout(new GridLayout(6, false));

    // Create the back button
    Button button = new Button(controls, SWT.PUSH);
    button.setText("Back");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.back();
        }
    });

    // Create the forward button
    button = new Button(controls, SWT.PUSH);
    button.setText("Forward");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.forward();
        }
    });

    // Create the refresh button
    button = new Button(controls, SWT.PUSH);
    button.setText("Refresh");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.refresh();
        }
    });

    // Create the stop button
    button = new Button(controls, SWT.PUSH);
    button.setText("Stop");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.stop();
        }
    });

    // Create the address entry field and set focus to it
    final Text url = new Text(controls, SWT.BORDER);
    url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    url.setFocus();

    // Create the go button
    button = new Button(controls, SWT.PUSH);
    button.setText("Go");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            browser.setUrl(url.getText());
        }
    });

    // Allow users to hit enter to go to the typed URL
    shell.setDefaultButton(button);
}