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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 225");
    Image image = null;/*w w  w  .  ja v a2s  . c  o  m*/
    final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
    tip.setMessage(
            "Here is a message for the user. When the message is too long it wraps. I should say something cool but nothing comes to my mind.");
    Tray tray = display.getSystemTray();
    if (tray != null) {
        TrayItem item = new TrayItem(tray, SWT.NONE);
        image = display.getSystemImage(SWT.ICON_INFORMATION);
        item.setImage(image);
        tip.setText("Notification from a tray item");
        item.setToolTip(tip);
    } else {
        tip.setText("Notification from anywhere");
        tip.setLocation(400, 400);
    }
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Press for balloon tip");
    button.addListener(SWT.Selection, event -> tip.setVisible(true));
    Rectangle clientArea = shell.getClientArea();
    button.setLocation(clientArea.x, clientArea.y);
    button.pack();
    shell.setBounds(50, 50, 300, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (image != null)
        image.dispose();
    display.dispose();
}

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

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

    final Browser browser;
    try {/*w ww  . j a v  a  2 s. c o  m*/
        browser = new Browser(shell, SWT.NONE);
    } catch (SWTError e) {
        System.out.println("Could not instantiate Browser: " + e.getMessage());
        display.dispose();
        return;
    }
    browser.setLayoutData(new GridData(400, 400));
    browser.setText(HTML);

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Invoke Browser.close()");
    button.addListener(SWT.Selection, event -> {
        boolean result = browser.close();
        System.out.println("was Browser disposed: " + result);
        if (result) {
            button.setEnabled(false);
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 317");
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;/*from  w  ww  .j  av a2 s  .c o  m*/
    shell.setLayout(gridLayout);
    final Text location = new Text(shell, SWT.BORDER);
    GridData data = new GridData();
    data.horizontalAlignment = GridData.FILL;
    data.grabExcessHorizontalSpace = true;
    location.setLayoutData(data);
    Button go = new Button(shell, SWT.PUSH);
    go.setText("Go");

    final Browser browser;
    try {
        browser = new Browser(shell, SWT.NONE);
    } catch (SWTError e) {
        System.out.println("Could not instantiate Browser: " + e.getMessage());
        display.dispose();
        return;
    }
    data = new GridData();
    data.horizontalAlignment = data.verticalAlignment = GridData.FILL;
    data.grabExcessHorizontalSpace = data.grabExcessVerticalSpace = true;
    data.horizontalSpan = 2;
    browser.setLayoutData(data);
    browser.setUrl("eclipse.org");
    browser.addLocationListener(new LocationAdapter() {
        @Override
        public void changed(LocationEvent event) {
            location.setText(event.location);
        }
    });

    Listener navigateListener = event -> browser.setUrl(location.getText());
    go.addListener(SWT.Selection, navigateListener);
    location.addListener(SWT.DefaultSelection, navigateListener);

    browser.addAuthenticationListener(event -> {
        try {
            URL url = new URL(event.location);
            if (url.getHost().equals(KNOWN_HOST)) {
                event.user = KNOWN_USER;
                event.password = KNOWN_PASSWORD;
            } else {
                /* do nothing, let default prompter run */
            }
        } catch (MalformedURLException e) {
            /* should not happen, let default prompter run */
        }
    });

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

From source file:MainClass.java

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

    s.setSize(250, 200);/* w  w  w  .java2  s.c om*/
    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);
    TableItem item1 = new TableItem(t, SWT.NONE);
    item1.setText(new String[] { "A", "B", "Address 1" });
    TableItem item2 = new TableItem(t, SWT.NONE);
    item2.setText(new String[] { "C", "D", "Address 2" });
    TableItem item3 = new TableItem(t, SWT.NONE);
    item3.setText(new String[] { "E", "F", "Address 3" });

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
    FillLayout layout1 = new FillLayout(SWT.VERTICAL);
    layout1.marginWidth = layout1.marginHeight = 10;
    shell.setLayout(layout1);/*from  ww w  . ja v a2  s  . c  o  m*/
    Group group = new Group(shell, SWT.NONE);
    group.setText("Group ");
    RowLayout layout2 = new RowLayout(SWT.VERTICAL);
    layout2.marginWidth = layout2.marginHeight = layout2.spacing = 10;
    group.setLayout(layout2);
    for (int i = 0; i < 8; i++) {
        Button button = new Button(group, SWT.RADIO);
        button.setText("Button " + i);
    }
    shell.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle rect = shell.getClientArea();
            Image newImage = new Image(display, Math.max(1, rect.width), 1);
            GC gc = new GC(newImage);
            gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
            gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
            gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
            gc.dispose();
            shell.setBackgroundImage(newImage);
            if (oldImage != null)
                oldImage.dispose();
            oldImage = newImage;
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (oldImage != null)
        oldImage.dispose();
    display.dispose();
}

From source file:MainClass.java

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

    s.setSize(250, 200);//from  w w w  .j  a  va2  s . co  m
    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);
    TableItem item1 = new TableItem(t, SWT.NONE);
    item1.setText(new String[] { "A", "B", "Address 1" });
    TableItem item2 = new TableItem(t, SWT.NONE);
    item2.setText(new String[] { "C", "D", "Address 2" });
    TableItem item3 = new TableItem(t, SWT.NONE);
    item3.setText(new String[] { "E", "F", "Address 3" });

    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());
                    tia[i].setBackground(new Color(d, 127, 178, 127));
                }
            }
        }
    });

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

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

public static void main(String[] arg) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Skin example");
    shell.setLayout(new GridLayout());

    Group container = new Group(shell, SWT.None);
    container.setText("Container");
    container.setLayout(new GridLayout(3, false));

    Composite child1 = new Composite(container, SWT.BORDER);
    child1.setLayout(new GridLayout());
    new Label(child1, SWT.NONE).setText("Label in pane 1");

    Composite child2 = new Composite(container, SWT.BORDER);
    child2.setLayout(new GridLayout());
    new Button(child2, SWT.PUSH).setText("Button in pane2");

    final Composite child3 = new Composite(container, SWT.BORDER);
    child3.setLayout(new GridLayout());
    new Text(child3, SWT.BORDER).setText("Text in pane3");

    display.addListener(SWT.Skin, event -> {
        System.out.println("Skin: " + event.widget);
        setBackground(event.display, (Control) event.widget);
    });//  w w  w. j ava2  s .c  om

    Composite buttonContainer = new Composite(shell, SWT.NONE);
    buttonContainer.setLayout(new GridLayout(3, false));
    Button reskin = new Button(buttonContainer, SWT.PUSH);
    reskin.setText("Reskin All");
    reskin.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        shell.reskin(SWT.ALL);
    }));
    Button reskin2 = new Button(buttonContainer, SWT.PUSH);
    reskin2.setText("Reskin Shell");
    reskin2.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        shell.reskin(SWT.None);
    }));
    Button reskin3 = new Button(buttonContainer, SWT.PUSH);
    reskin3.setText("Reskin Right Composite");
    reskin3.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        child3.reskin(SWT.ALL);
    }));

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

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

public static void main(String[] args) {
    Display display = new Display();
    Color red = display.getSystemColor(SWT.COLOR_RED);
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    Color black = display.getSystemColor(SWT.COLOR_BLACK);

    Image image = new Image(display, 20, 20);
    GC gc = new GC(image);
    gc.setBackground(red);/*  www.j  a v  a  2  s .co  m*/
    gc.fillRectangle(5, 5, 10, 10);
    gc.dispose();
    ImageData imageData = image.getImageData();

    PaletteData palette = new PaletteData(new RGB(0, 0, 0), new RGB(0xFF, 0xFF, 0xFF));
    ImageData maskData = new ImageData(20, 20, 1, palette);
    Image mask = new Image(display, maskData);
    gc = new GC(mask);
    gc.setBackground(black);
    gc.fillRectangle(0, 0, 20, 20);
    gc.setBackground(white);
    gc.fillRectangle(5, 5, 10, 10);
    gc.dispose();
    maskData = mask.getImageData();

    Image icon = new Image(display, imageData, maskData);
    Shell shell = new Shell(display);
    shell.setText("Snippet 70");
    Button button = new Button(shell, SWT.PUSH);
    button.setImage(icon);
    button.setSize(60, 60);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    icon.dispose();
    image.dispose();
    mask.dispose();
    display.dispose();
}

From source file:MainClass.java

public static void main(String[] a) {

    final Display d = new Display();
    final Shell shell = new Shell(d);

    shell.setSize(250, 200);//from w  w  w . j  a  v a2  s  . co  m

    shell.setLayout(new FillLayout());
    final int NUM = 5;

    final String[] options = { "Option 1", "Option 2", "Option 3" };

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

    for (int i = 0; i < NUM; i++) {
        TableColumn column = new TableColumn(table, SWT.CENTER);
        column.setText("Column " + (i + 1));
        column.pack();
    }

    TableEditor[] colorEditors = new TableEditor[NUM];

    Button[] colorButtons = new Button[NUM];

    for (int i = 0; i < NUM; i++) {
        final TableItem item = new TableItem(table, SWT.NONE);

        colorEditors[i] = new TableEditor(table);
        colorButtons[i] = new Button(table, SWT.PUSH);

        colorEditors[i].setEditor(colorButtons[i], item, 0);
    }

    final TableEditor editor = new TableEditor(table);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;

    table.addMouseListener(new MouseAdapter() {
        public void mouseDown(MouseEvent event) {
            Control old = editor.getEditor();
            if (old != null)
                old.dispose();

            Point pt = new Point(event.x, event.y);

            final TableItem item = table.getItem(pt);
            if (item != null) {
                int column = -1;
                for (int i = 0, n = table.getColumnCount(); i < n; i++) {
                    Rectangle rect = item.getBounds(i);
                    if (rect.contains(pt)) {
                        column = i;
                        break;
                    }
                }
                if (column == 1) {
                    final CCombo combo = new CCombo(table, SWT.READ_ONLY);
                    for (int i = 0, n = options.length; i < n; i++) {
                        combo.add(options[i]);
                    }

                    combo.select(combo.indexOf(item.getText(column)));

                    editor.minimumWidth = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
                    table.getColumn(column).setWidth(editor.minimumWidth);

                    combo.setFocus();
                    editor.setEditor(combo, item, column);

                } else if (column > 1) {
                    final Text text = new Text(table, SWT.NONE);
                    text.setForeground(item.getForeground());

                    text.setText(item.getText(column));
                    text.setForeground(item.getForeground());
                    text.selectAll();
                    text.setFocus();

                    editor.minimumWidth = text.getBounds().width;

                    editor.setEditor(text, item, column);

                }
            }
        }
    });

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

From source file:Snippet115.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    Composite c1 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c1.setLayout(new RowLayout());
    Composite c2 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c2.setLayout(new RowLayout());
    final Composite[] composites = new Composite[] { c1, c2 };
    Listener radioGroup = new Listener() {
        public void handleEvent(Event event) {
            for (int i = 0; i < composites.length; i++) {
                Composite composite = composites[i];
                Control[] children = composite.getChildren();
                for (int j = 0; j < children.length; j++) {
                    Control child = children[j];
                    if (child instanceof Button) {
                        Button button = (Button) child;
                        if ((button.getStyle() & SWT.RADIO) != 0)
                            button.setSelection(false);
                    }/*from   ww w .j av a 2 s  . co  m*/
                }
            }
            Button button = (Button) event.widget;
            button.setSelection(true);
        }
    };
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c1, SWT.RADIO);
        button.setText("Button " + i);
        button.addListener(SWT.Selection, radioGroup);
    }
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c2, SWT.RADIO);
        button.setText("Button " + (i + 4));
        button.addListener(SWT.Selection, radioGroup);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}