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

/** * Buttons ** */

public static Button createButton(Composite composite) {
    final Button button = new Button(composite, SWT.PUSH);
    button.setText("Press me!");
    //       React to click events
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Key was pressed");
        }/*from  w  w w  . j a v  a 2 s.  co m*/
    });
    return button;
}

From source file:ShowCTabFolder.java

/**
 * Creates the buttons for moving the insert mark and adding a tab
 * /*from  w  w  w .j  a va 2  s .  c o  m*/
 * @param composite the parent composite
 */
private void createButtons(Composite composite) {
    // Move mark left
    Button button = new Button(composite, SWT.PUSH);
    button.setText("<<");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            if (insertMark > -1) {
                --insertMark;
                resetInsertMark();
            }
        }
    });

    // Move mark right
    button = new Button(composite, SWT.PUSH);
    button.setText(">>");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            if (insertMark < tabFolder.getItemCount() - 1) {
                ++insertMark;
                resetInsertMark();
            }
        }
    });

    // Add a tab
    button = new Button(composite, SWT.PUSH);
    button.setText("Add Tab");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            new CTabItem(tabFolder, SWT.NONE, insertMark + 1).setText("Tab (" + (insertMark + 1) + ")");
        }
    });
}

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

/**
 * Creates the "Style" group.//from w w w  . ja va 2s.  c o m
 */
@Override
void createStyleGroup() {
    super.createStyleGroup();

    /* Create the extra widgets */
    borderButton = new Button(styleGroup, SWT.CHECK);
    borderButton.setText("SWT.BORDER");
}

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

/**
 * Creates the "Style" group./*from   ww  w  .  j  av a 2  s. co m*/
 */
@Override
void createStyleGroup() {
    super.createStyleGroup();

    /* Create the extra widgets */
    smoothButton = new Button(styleGroup, SWT.CHECK);
    smoothButton.setText("SWT.SMOOTH");
    indeterminateButton = new Button(styleGroup, SWT.CHECK);
    indeterminateButton.setText("SWT.INDETERMINATE");
}

From source file:org.eclipse.swt.examples.graphics.LineCapTab.java

@Override
public void createControlPanel(Composite parent) {

    Composite comp;//from   w  w w  . java  2  s . co  m

    // create color button
    comp = new Composite(parent, SWT.NONE);
    comp.setLayout(new GridLayout());

    ColorMenu cm = new ColorMenu();
    cm.setPatternItems(example.checkAdvancedGraphics());
    menu = cm.createMenu(parent.getParent(), gb -> {
        foreground = gb;
        colorButton.setImage(gb.getThumbNail());
        example.redraw();
    });

    // initialize the foreground to the 3rd item in the menu (red)
    foreground = (GraphicsBackground) menu.getItem(2).getData();

    // color button
    colorButton = new Button(comp, SWT.PUSH);
    colorButton.setText(GraphicsExample.getResourceString("Color")); //$NON-NLS-1$
    colorButton.setImage(foreground.getThumbNail());
    colorButton.addListener(SWT.Selection, event -> {
        final Button button = (Button) event.widget;
        final Composite parent1 = button.getParent();
        Rectangle bounds = button.getBounds();
        Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
        menu.setLocation(point.x, point.y + bounds.height);
        menu.setVisible(true);
    });
}

From source file:MainClass.java

public void createButton(Composite c, int style, String text) {

    Button b = new Button(c, style);

    b.setText(text);
}

From source file:SWTBrowser.java

public SWTBrowser() {
    shell.setLayout(new GridLayout());

    ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
    final ToolBarManager manager = new ToolBarManager(toolBar);

    Composite compositeLocation = new Composite(shell, SWT.NULL);
    compositeLocation.setLayout(new GridLayout(3, false));
    compositeLocation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Label labelAddress = new Label(compositeLocation, SWT.NULL);
    labelAddress.setText("Address");

    textLocation = new Text(compositeLocation, SWT.SINGLE | SWT.BORDER);
    textLocation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Button buttonGo = new Button(compositeLocation, SWT.NULL);
    buttonGo.setImage(new Image(shell.getDisplay(), "java2s.gif"));

    browser = new Browser(shell, SWT.BORDER);
    browser.setLayoutData(new GridData(GridData.FILL_BOTH));

    Composite compositeStatus = new Composite(shell, SWT.NULL);
    compositeStatus.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    compositeStatus.setLayout(new GridLayout(2, false));

    labelStatus = new Label(compositeStatus, SWT.NULL);
    labelStatus.setText("Ready");
    labelStatus.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    final ProgressBar progressBar = new ProgressBar(compositeStatus, SWT.SMOOTH);

    Listener openURLListener = new Listener() {
        public void handleEvent(Event event) {
            browser.setUrl(textLocation.getText());
        }/*from  w w  w .  java 2s  . com*/
    };

    buttonGo.addListener(SWT.Selection, openURLListener);
    textLocation.addListener(SWT.DefaultSelection, openURLListener);

    // Adds tool bar items using actions.
    final Action actionBackward = new Action("&Backword", ImageDescriptor.createFromFile(null, "java2s.gif")) {
        public void run() {
            browser.back();
        }
    };
    actionBackward.setEnabled(false); // action is disabled at start up.

    final Action actionForward = new Action("&Forward",
            ImageDescriptor.createFromFile(null, "icons/web/forward.gif")) {
        public void run() {
            browser.forward();
        }
    };
    actionForward.setEnabled(false); // action is disabled at start up.

    Action actionStop = new Action("&Stop", ImageDescriptor.createFromFile(null, "icons/web/stop.gif")) {
        public void run() {
            browser.stop();
        }
    };

    Action actionRefresh = new Action("&Refresh",
            ImageDescriptor.createFromFile(null, "icons/web/refresh.gif")) {
        public void run() {
            browser.refresh();
        }
    };

    Action actionHome = new Action("&Home", ImageDescriptor.createFromFile(null, "icons/web/home.gif")) {
        public void run() {
            browser.setUrl("http://www.eclipse.org");
        }
    };

    manager.add(actionBackward);
    manager.add(actionForward);
    manager.add(actionStop);
    manager.add(actionRefresh);
    manager.add(actionHome);

    manager.update(true);
    toolBar.pack();

    browser.addLocationListener(new LocationListener() {
        public void changing(LocationEvent event) {
            // Displays the new location in the text field.
            textLocation.setText(event.location);
        }

        public void changed(LocationEvent event) {
            // Update tool bar items.
            actionBackward.setEnabled(browser.isBackEnabled());
            actionForward.setEnabled(browser.isForwardEnabled());
            manager.update(false);
        }
    });

    browser.addProgressListener(new ProgressListener() {
        public void changed(ProgressEvent event) {
            progressBar.setMaximum(event.total);
            progressBar.setSelection(event.current);
        }

        public void completed(ProgressEvent event) {
            progressBar.setSelection(0);
        }
    });

    browser.addStatusTextListener(new StatusTextListener() {
        public void changed(StatusTextEvent event) {
            labelStatus.setText(event.text);
        }
    });

    browser.addTitleListener(new TitleListener() {
        public void changed(TitleEvent event) {
            shell.setText(event.title + " - powered by SWT");
        }
    });

    initialize(display, browser);

    shell.setSize(500, 400);
    shell.open();
    //textUser.forceFocus();

    //browser.setText(
    //   "<html><body>" + "<h1>SWT &amp; JFace </h1>" + "</body/html>");

    // 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:TextTableEditor.java

/**
 * Creates the main window's contents/*from ww  w. j ava2s  .  c o  m*/
 * 
 * @param shell the main window
 */
private void createContents(final Shell shell) {
    shell.setLayout(new FillLayout());

    // Create the table
    final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    // Create five columns
    for (int i = 0; i < NUM; 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[NUM];

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

    // Create five rows and the editors for those rows. The first column has the
    // color change buttons. The second column has dropdowns. The final three
    // have text fields.
    for (int i = 0; i < NUM; i++) {
        // Create the row
        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);

        // Create a handler for the button
        final int index = i;
        colorButtons[i].addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                ColorDialog dialog = new ColorDialog(shell);
                if (colors[index] != null)
                    dialog.setRGB(colors[index].getRGB());
                RGB rgb = dialog.open();
                if (rgb != null) {
                    if (colors[index] != null)
                        colors[index].dispose();
                    colors[index] = new Color(shell.getDisplay(), rgb);
                    item.setForeground(colors[index]);
                }
            }
        });
    }

    // Create an editor object to use for text editing
    final TableEditor editor = new TableEditor(table);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;

    // Use a mouse listener, not a selection listener, since we're interested
    // in the selected column as well as row
    table.addMouseListener(new MouseAdapter() {
        public void mouseDown(MouseEvent event) {
            // Dispose any existing editor
            Control old = editor.getEditor();
            if (old != null)
                old.dispose();

            // Determine where the mouse was clicked
            Point pt = new Point(event.x, event.y);

            // Determine which row was selected
            final TableItem item = table.getItem(pt);
            if (item != null) {
                // Determine which column was selected
                int column = -1;
                for (int i = 0, n = table.getColumnCount(); i < n; i++) {
                    Rectangle rect = item.getBounds(i);
                    if (rect.contains(pt)) {
                        // This is the selected column
                        column = i;
                        break;
                    }
                }

                // Column 2 holds dropdowns
                if (column == 1) {
                    // Create the dropdown and add data to it
                    final CCombo combo = new CCombo(table, SWT.READ_ONLY);
                    for (int i = 0, n = options.length; i < n; i++) {
                        combo.add(options[i]);
                    }

                    // Select the previously selected item from the cell
                    combo.select(combo.indexOf(item.getText(column)));

                    // Compute the width for the editor
                    // Also, compute the column width, so that the dropdown fits
                    editor.minimumWidth = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
                    table.getColumn(column).setWidth(editor.minimumWidth);

                    // Set the focus on the dropdown and set into the editor
                    combo.setFocus();
                    editor.setEditor(combo, item, column);

                    // Add a listener to set the selected item back into the cell
                    final int col = column;
                    combo.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent event) {
                            item.setText(col, combo.getText());

                            // They selected an item; end the editing session
                            combo.dispose();
                        }
                    });
                } else if (column > 1) {
                    // Create the Text object for our editor
                    final Text text = new Text(table, SWT.NONE);
                    text.setForeground(item.getForeground());

                    // Transfer any text from the cell to the Text control,
                    // set the color to match this row, select the text,
                    // and set focus to the control
                    text.setText(item.getText(column));
                    text.setForeground(item.getForeground());
                    text.selectAll();
                    text.setFocus();

                    // Recalculate the minimum width for the editor
                    editor.minimumWidth = text.getBounds().width;

                    // Set the control into the editor
                    editor.setEditor(text, item, column);

                    // Add a handler to transfer the text back to the cell
                    // any time it's modified
                    final int col = column;
                    text.addModifyListener(new ModifyListener() {
                        public void modifyText(ModifyEvent event) {
                            // Set the text of the editor's control back into the cell
                            item.setText(col, text.getText());
                        }
                    });
                }
            }
        }
    });
}

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

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

    /* Create display controls specific to this example */
    caretButton = new Button(otherGroup, SWT.CHECK);
    caretButton.setText(ControlExample.getResourceString("Caret"));
    fillDamageButton = new Button(otherGroup, SWT.CHECK);
    fillDamageButton.setText(ControlExample.getResourceString("FillDamage"));

    /* Add the listeners */
    caretButton.addSelectionListener(widgetSelectedAdapter(event -> setCaret()));
}

From source file:ShowMessageBox.java

/**
 * Creates the main window's contents/*from  w w w. j a  va2s.  c  o m*/
 * 
 * @param shell the parent shell
 */
private void createContents(final Shell shell) {
    shell.setLayout(new GridLayout(2, false));

    // Create the dropdown to allow icon selection
    new Label(shell, SWT.NONE).setText("Icon:");
    final Combo icons = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    for (int i = 0, n = ICONS.length; i < n; i++)
        icons.add(ICONS[i]);
    icons.select(0);

    // Create the dropdown to allow button selection
    new Label(shell, SWT.NONE).setText("Buttons:");
    final Combo buttons = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    for (int i = 0, n = BUTTONS.length; i < n; i++)
        buttons.add(BUTTONS[i]);
    buttons.select(0);

    // Create the entry field for the message
    new Label(shell, SWT.NONE).setText("Message:");
    final Text message = new Text(shell, SWT.BORDER);
    message.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // Create the label to show the return from the open call
    new Label(shell, SWT.NONE).setText("Return:");
    final Label returnVal = new Label(shell, SWT.NONE);
    returnVal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // Create the button and event handler
    // to display the message box
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Show Message");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            // Clear any previously returned value
            returnVal.setText("");

            // This will hold the style to pass to the MessageBox constructor
            int style = 0;

            // Determine which icon was selected and
            // add it to the style
            switch (icons.getSelectionIndex()) {
            case 0:
                style |= SWT.ICON_ERROR;
                break;
            case 1:
                style |= SWT.ICON_INFORMATION;
                break;
            case 2:
                style |= SWT.ICON_QUESTION;
                break;
            case 3:
                style |= SWT.ICON_WARNING;
                break;
            case 4:
                style |= SWT.ICON_WORKING;
                break;
            }

            // Determine which set of buttons was selected
            // and add it to the style
            switch (buttons.getSelectionIndex()) {
            case 0:
                style |= SWT.OK;
                break;
            case 1:
                style |= SWT.OK | SWT.CANCEL;
                break;
            case 2:
                style |= SWT.YES | SWT.NO;
                break;
            case 3:
                style |= SWT.YES | SWT.NO | SWT.CANCEL;
                break;
            case 4:
                style |= SWT.RETRY | SWT.CANCEL;
                break;
            case 5:
                style |= SWT.ABORT | SWT.RETRY | SWT.IGNORE;
                break;
            }

            // Display the message box
            MessageBox mb = new MessageBox(shell, style);
            mb.setText("Message from SWT");
            mb.setMessage(message.getText());
            int val = mb.open();
            String valString = "";
            switch (val) // val contains the constant of the selected button
            {
            case SWT.OK:
                valString = "SWT.OK";
                break;
            case SWT.CANCEL:
                valString = "SWT.CANCEL";
                break;
            case SWT.YES:
                valString = "SWT.YES";
                break;
            case SWT.NO:
                valString = "SWT.NO";
                break;
            case SWT.RETRY:
                valString = "SWT.RETRY";
                break;
            case SWT.ABORT:
                valString = "SWT.ABORT";
                break;
            case SWT.IGNORE:
                valString = "SWT.IGNORE";
                break;
            }
            returnVal.setText(valString);
        }
    });
}