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:IconInMemory.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);/*from   w ww. j  ava  2 s . c  om*/
    gc.fillRectangle(5, 5, 10, 10);
    gc.dispose();
    ImageData imageData = image.getImageData();

    PaletteData palette = new PaletteData(new RGB[] { 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);
    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:org.eclipse.swt.snippets.Snippet217.java

public static void main(String[] args) {
    final Display display = new Display();
    Font font = new Font(display, "Tahoma", 16, SWT.NORMAL);
    final Shell shell = new Shell(display);
    shell.setText("Snippet 217");
    shell.setLayout(new GridLayout());
    styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setFont(font);/*from   ww  w.j a  v a  2  s .  c o m*/
    styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    styledText.setText(text);
    Button button = new Button(styledText, SWT.PUSH);
    button.setText("Button 1");
    int offset = text.indexOf('\uFFFC');
    addControl(button, offset);
    button.setLocation(styledText.getLocationAtOffset(offset));
    Combo combo = new Combo(styledText, SWT.NONE);
    combo.add("item 1");
    combo.add("another item");
    combo.setText(combo.getItem(0));
    offset = text.indexOf('\uFFFC', offset + 1);
    addControl(combo, offset);
    combo.setLocation(styledText.getLocationAtOffset(offset));

    // use a verify listener to dispose the controls
    styledText.addVerifyListener(event -> {
        if (event.start == event.end)
            return;
        String text = styledText.getText(event.start, event.end - 1);
        int index = text.indexOf('\uFFFC');
        while (index != -1) {
            StyleRange style = styledText.getStyleRangeAtOffset(event.start + index);
            if (style != null) {
                Control control = (Control) style.data;
                if (control != null)
                    control.dispose();
            }
            index = text.indexOf('\uFFFC', index + 1);
        }
    });

    // reposition widgets on paint event
    styledText.addPaintObjectListener(event -> {
        Control control = (Control) event.style.data;
        Point pt = control.getSize();
        int x = event.x + MARGIN;
        int y = event.y + event.ascent - 2 * pt.y / 3;
        control.setLocation(x, y);
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 361");
    shell.setText("Translate and Rotate an AWT Image in an SWT GUI");
    shell.setLayout(new GridLayout(8, false));

    Button fileButton = new Button(shell, SWT.PUSH);
    fileButton.setText("&Open Image File");
    fileButton.addSelectionListener(widgetSelectedAdapter(e -> {
        String filename = new FileDialog(shell).open();
        if (filename != null) {
            image = Toolkit.getDefaultToolkit().getImage(filename);
            canvas.repaint();/*from w ww .  j  ava  2 s.com*/
        }
    }));

    new Label(shell, SWT.NONE).setText("Translate &X by:");
    final Combo translateXCombo = new Combo(shell, SWT.NONE);
    translateXCombo.setItems("0", "image width", "image height", "100", "200");
    translateXCombo.select(0);
    translateXCombo.addModifyListener(e -> {
        translateX = numericValue(translateXCombo);
        canvas.repaint();
    });

    new Label(shell, SWT.NONE).setText("Translate &Y by:");
    final Combo translateYCombo = new Combo(shell, SWT.NONE);
    translateYCombo.setItems("0", "image width", "image height", "100", "200");
    translateYCombo.select(0);
    translateYCombo.addModifyListener(e -> {
        translateY = numericValue(translateYCombo);
        canvas.repaint();
    });

    new Label(shell, SWT.NONE).setText("&Rotate by:");
    final Combo rotateCombo = new Combo(shell, SWT.NONE);
    rotateCombo.setItems("0", "Pi", "Pi/2", "Pi/4", "Pi/8");
    rotateCombo.select(0);
    rotateCombo.addModifyListener(e -> {
        rotate = numericValue(rotateCombo);
        canvas.repaint();
    });

    Button printButton = new Button(shell, SWT.PUSH);
    printButton.setText("&Print Image");
    printButton.addSelectionListener(widgetSelectedAdapter(e -> {
        performPrintAction(display, shell);
    }));

    composite = new Composite(shell, SWT.EMBEDDED | SWT.BORDER);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 8, 1);
    data.widthHint = 640;
    data.heightHint = 480;
    composite.setLayoutData(data);
    Frame frame = SWT_AWT.new_Frame(composite);
    canvas = new Canvas() {
        @Override
        public void paint(Graphics g) {
            if (image != null) {
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());

                /* Use Java2D here to modify the image as desired. */
                Graphics2D g2d = (Graphics2D) g;
                AffineTransform t = new AffineTransform();
                t.translate(translateX, translateY);
                t.rotate(rotate);
                g2d.setTransform(t);
                /*------------*/

                g.drawImage(image, 0, 0, this);
            }
        }
    };
    frame.add(canvas);
    composite.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getName(AccessibleEvent e) {
            e.result = "Image drawn in AWT Canvas";
        }
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 181");
    shell.setLayout(new RowLayout(SWT.HORIZONTAL));
    final Table table = new Table(shell, SWT.BORDER | SWT.CHECK);
    table.setLayoutData(new RowData(-1, 300));
    table.setHeaderVisible(true);/*from w w  w . j av  a 2s . com*/
    TableColumn column = new TableColumn(table, SWT.LEFT);
    column.setText("Column 0");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 1");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 2");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 3");
    column = new TableColumn(table, SWT.CENTER);
    column.setText("Column 4");
    for (int i = 0; i < 100; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        String[] text = new String[] { i + " 0", i + " 1", i + " 2", i + " 3", i + " 4" };
        item.setText(text);
    }
    Listener listener = e -> System.out.println("Move " + e.widget);
    TableColumn[] columns = table.getColumns();
    for (int i = 0; i < columns.length; i++) {
        columns[i].pack();
        columns[i].setMoveable(true);
        columns[i].addListener(SWT.Move, listener);
    }
    Button b = new Button(shell, SWT.PUSH);
    b.setText("invert column order");
    b.addListener(SWT.Selection, e -> {
        int[] order = table.getColumnOrder();
        for (int i = 0; i < order.length / 2; i++) {
            int temp = order[i];
            order[i] = order[order.length - i - 1];
            order[order.length - i - 1] = temp;
        }
        table.setColumnOrder(order);
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TableCellEditorComboTextButton.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
    table.setLinesVisible(true);/*from  w  w  w. j a  v  a2 s.c om*/
    for (int i = 0; i < 3; i++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setWidth(100);
    }
    for (int i = 0; i < 12; i++) {
        new TableItem(table, SWT.NONE);
    }
    TableItem[] items = table.getItems();
    for (int i = 0; i < items.length; i++) {
        TableEditor editor = new TableEditor(table);
        CCombo combo = new CCombo(table, SWT.NONE);
        combo.setText("CCombo");
        combo.add("item 1");
        combo.add("item 2");
        editor.grabHorizontal = true;
        editor.setEditor(combo, items[i], 0);
        editor = new TableEditor(table);
        Text text = new Text(table, SWT.NONE);
        text.setText("Text");
        editor.grabHorizontal = true;
        editor.setEditor(text, items[i], 1);
        editor = new TableEditor(table);
        Button button = new Button(table, SWT.CHECK);
        button.pack();
        editor.minimumWidth = button.getSize().x;
        editor.horizontalAlignment = SWT.LEFT;
        editor.setEditor(button, items[i], 2);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

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

    TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
    tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

    TabItem item = new TabItem(tabFolder, SWT.NONE);
    item.setText("Widget");
    Composite composite = new Composite(tabFolder, SWT.NONE);
    composite.setLayout(new GridLayout());
    Tree tree = new Tree(composite, SWT.BORDER);
    item.setControl(composite);//from  w  w w .  ja va 2 s  . c  o m
    tree.setHeaderVisible(true);
    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
    column1.setText("Standard");
    TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
    column2.setText("Widget");
    TreeItem branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Efficient", "Portable" });
    TreeItem leaf = new TreeItem(branch, SWT.NONE);
    leaf.setText(new String[] { "Cross", "Platform" });
    branch.setExpanded(true);
    branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Native", "Controls" });
    leaf = new TreeItem(branch, SWT.NONE);
    leaf.setText(new String[] { "Cross", "Platform" });
    branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Cross", "Platform" });
    column1.pack();
    column2.pack();

    item = new TabItem(tabFolder, SWT.NONE);
    item.setText("Toolkit");

    Button button = new Button(shell, SWT.CHECK);
    button.setText("Totally");
    button.setSelection(true);
    button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

    button = new Button(shell, SWT.PUSH);
    button.setText("Awesome");
    button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

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

From source file:org.eclipse.swt.examples.accessibility.AccessibleActionExample.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("Accessible Action Example");

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

    final Canvas customButton = new Canvas(shell, SWT.NONE) {
        @Override// ww w .j a v  a2 s . co m
        public Point computeSize(int wHint, int hHint, boolean changed) {
            GC gc = new GC(this);
            Point point = gc.stringExtent(buttonText);
            gc.dispose();
            point.x += MARGIN;
            point.y += MARGIN;
            return point;
        }
    };
    customButton.addPaintListener(e -> {
        Rectangle clientArea = customButton.getClientArea();
        Point stringExtent = e.gc.stringExtent(buttonText);
        int x = clientArea.x + (clientArea.width - stringExtent.x) / 2;
        int y = clientArea.y + (clientArea.height - stringExtent.y) / 2;
        e.gc.drawString(buttonText, x, y);
    });
    customButton.addMouseListener(MouseListener.mouseDownAdapter(e -> {
        int actionIndex = (e.button == 1) ? 0 : 1;
        customButtonAction(actionIndex);
    }));
    customButton.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            int modifierKeys = e.stateMask & SWT.MODIFIER_MASK;
            if (modifierKeys == SWT.CTRL || modifierKeys == 0) {
                if (e.character == '1')
                    customButtonAction(0);
                else if (e.character == '2')
                    customButtonAction(1);
            }
        }
    });

    Accessible accessible = customButton.getAccessible();
    accessible.addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getName(AccessibleEvent e) {
            e.result = buttonText;
        }

        @Override
        public void getKeyboardShortcut(AccessibleEvent e) {
            e.result = "CTRL+1"; // default action is 'action 1'
        }
    });
    accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
        @Override
        public void getRole(AccessibleControlEvent e) {
            e.detail = ACC.ROLE_PUSHBUTTON;
        }
    });
    accessible.addAccessibleActionListener(new AccessibleActionAdapter() {
        @Override
        public void getActionCount(AccessibleActionEvent e) {
            e.count = 2;
        }

        @Override
        public void getName(AccessibleActionEvent e) {
            if (0 <= e.index && e.index <= 1) {
                if (e.localized) {
                    e.result = AccessibleActionExample.getResourceString("action" + e.index);
                } else {
                    e.result = "Action" + e.index; //$NON-NLS-1$
                }
            }
        }

        @Override
        public void getDescription(AccessibleActionEvent e) {
            if (0 <= e.index && e.index <= 1) {
                e.result = AccessibleActionExample.getResourceString("action" + e.index + "description");
            }
        }

        @Override
        public void doAction(AccessibleActionEvent e) {
            if (0 <= e.index && e.index <= 1) {
                customButtonAction(e.index);
                e.result = ACC.OK;
            }
        }

        @Override
        public void getKeyBinding(AccessibleActionEvent e) {
            switch (e.index) {
            case 0:
                e.result = "1;CTRL+1";
                break;
            case 1:
                e.result = "2;CTRL+2";
                break;
            default:
                e.result = null;
            }
        }
    });

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

From source file:Snippet134.java

public static void main(String[] args) {
    final Display display = new Display();
    // Shell must be created with style SWT.NO_TRIM
    final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
    shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
    // define a region that looks like a key hole
    Region region = new Region();
    region.add(circle(67, 67, 67));//ww  w  .j a  v a  2s  . c om
    region.subtract(circle(20, 67, 50));
    region.subtract(new int[] { 67, 50, 55, 105, 79, 105 });
    // define the shape of the shell using setRegion
    shell.setRegion(region);
    Rectangle size = region.getBounds();
    shell.setSize(size.width, size.height);
    // add ability to move shell around
    Listener l = new Listener() {
        Point origin;

        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.MouseDown:
                origin = new Point(e.x, e.y);
                break;
            case SWT.MouseUp:
                origin = null;
                break;
            case SWT.MouseMove:
                if (origin != null) {
                    Point p = display.map(shell, null, e.x, e.y);
                    shell.setLocation(p.x - origin.x, p.y - origin.y);
                }
                break;
            }
        }
    };
    shell.addListener(SWT.MouseDown, l);
    shell.addListener(SWT.MouseUp, l);
    shell.addListener(SWT.MouseMove, l);
    // add ability to close shell
    Button b = new Button(shell, SWT.PUSH);
    b.setBackground(shell.getBackground());
    b.setText("close");
    b.pack();
    b.setLocation(10, 68);
    b.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            shell.close();
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    region.dispose();
    display.dispose();
}

From source file:ViewFormCreate.java

public static void main(String[] args) {

    Shell shell = new Shell(display);
    shell.setText("Look");

    shell.setLayout(new GridLayout(1, false));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("New Document");

    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    composite.setLayout(new FillLayout());

    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            createViewFormHelper(composite, "Document " + (++count));
            composite.layout();//  w ww.  ja v  a2  s  .  c  om
        }
    });

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

From source file:StyledTextEmbebControls.java

public static void main(String[] args) {
    final Display display = new Display();
    Font font = new Font(display, "Tahoma", 32, SWT.NORMAL);
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setFont(font);/* www  . j av a  2s  .  com*/
    styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    styledText.setText(text);
    controls = new Control[2];
    Button button = new Button(styledText, SWT.PUSH);
    button.setText("Button 1");
    controls[0] = button;
    Combo combo = new Combo(styledText, SWT.NONE);
    combo.add("item 1");
    combo.add("another item");
    controls[1] = combo;
    offsets = new int[controls.length];
    int lastOffset = 0;
    for (int i = 0; i < controls.length; i++) {
        int offset = text.indexOf("\uFFFC", lastOffset);
        offsets[i] = offset;
        addControl(controls[i], offsets[i]);
        lastOffset = offset + 1;
    }

    // use a verify listener to keep the offsets up to date
    styledText.addVerifyListener(new VerifyListener() {
        public void verifyText(VerifyEvent e) {
            int start = e.start;
            int replaceCharCount = e.end - e.start;
            int newCharCount = e.text.length();
            for (int i = 0; i < offsets.length; i++) {
                int offset = offsets[i];
                if (start <= offset && offset < start + replaceCharCount) {
                    // this widget is being deleted from the text
                    if (controls[i] != null && !controls[i].isDisposed()) {
                        controls[i].dispose();
                        controls[i] = null;
                    }
                    offset = -1;
                }
                if (offset != -1 && offset >= start)
                    offset += newCharCount - replaceCharCount;
                offsets[i] = offset;
            }
        }
    });

    // reposition widgets on paint event
    styledText.addPaintObjectListener(new PaintObjectListener() {
        public void paintObject(PaintObjectEvent event) {
            StyleRange style = event.style;
            int start = style.start;
            for (int i = 0; i < offsets.length; i++) {
                int offset = offsets[i];
                if (start == offset) {
                    Point pt = controls[i].getSize();
                    int x = event.x + MARGIN;
                    int y = event.y + event.ascent - 2 * pt.y / 3;
                    controls[i].setLocation(x, y);
                    break;
                }
            }
        }
    });

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