Example usage for org.eclipse.swt.widgets Button setText

List of usage examples for org.eclipse.swt.widgets Button setText

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button setText.

Prototype

public void setText(String text) 

Source Link

Document

Sets the receiver's text.

Usage

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

public static void main(String[] arg) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 334");
    shell.setLayout(new GridLayout(2, false));
    Label label = new Label(shell, SWT.NONE);
    label.setText("Test:");
    canvas = new Canvas(shell, SWT.MULTI | SWT.BORDER);
    final Caret caret = new Caret(canvas, SWT.NONE);
    canvas.addPaintListener(e -> {/*  ww  w.  j  a  v  a  2 s.c o m*/
        GC gc = e.gc;
        gc.drawText(text, 10, 10);
        caret.setBounds(10, 10, 2, gc.getFontMetrics().getHeight());
        Rectangle rect = canvas.getClientArea();
        if (canvas.isFocusControl()) {
            gc.drawFocus(rect.x, rect.y, rect.width, rect.height);
        }
    });
    canvas.addTraverseListener(e -> {
        switch (e.detail) {
        case SWT.TRAVERSE_TAB_NEXT:
        case SWT.TRAVERSE_TAB_PREVIOUS:
            e.doit = true; // enable traversal
            break;
        }
    });

    // key listener enables traversal out)
    canvas.addKeyListener(keyPressedAdapter(e -> {
    }));

    canvas.addFocusListener(focusGainedAdapter(event -> canvas.redraw()));
    canvas.addFocusListener(focusLostAdapter(event -> canvas.redraw()));
    canvas.addMouseListener(mouseDownAdapter(e -> canvas.setFocus()));
    Accessible acc = canvas.getAccessible();
    acc.addRelation(ACC.RELATION_LABELLED_BY, label.getAccessible());
    acc.addAccessibleControlListener(new AccessibleControlAdapter() {
        @Override
        public void getRole(AccessibleControlEvent e) {
            e.detail = ACC.ROLE_TEXT;
        }

        @Override
        public void getLocation(AccessibleControlEvent e) {
            Rectangle rect = canvas.getBounds();
            Point pt = shell.toDisplay(rect.x, rect.y);
            e.x = pt.x;
            e.y = pt.y;
            e.width = rect.width;
            e.height = rect.height;
        }

        @Override
        public void getValue(AccessibleControlEvent e) {
            e.result = text;
        }

        @Override
        public void getFocus(AccessibleControlEvent e) {
            e.childID = ACC.CHILDID_SELF;
        }

        @Override
        public void getChildCount(AccessibleControlEvent e) {
            e.detail = 0;
        }

        @Override
        public void getState(AccessibleControlEvent e) {
            e.detail = ACC.STATE_NORMAL | ACC.STATE_FOCUSABLE;
            if (canvas.isFocusControl())
                e.detail |= ACC.STATE_FOCUSED | ACC.STATE_SELECTABLE;
        }
    });
    acc.addAccessibleTextListener(new AccessibleTextExtendedAdapter() {
        @Override
        public void getSelectionRange(AccessibleTextEvent e) {
            // select the first 4 characters for testing
            e.offset = 0;
            e.length = 4;
        }

        @Override
        public void getCaretOffset(AccessibleTextEvent e) {
            e.offset = 0;
        }

        @Override
        public void getTextBounds(AccessibleTextEvent e) {
            // for now, assume that start = 0 and end = text.length
            GC gc = new GC(canvas);
            Point extent = gc.textExtent(text);
            gc.dispose();
            Rectangle rect = display.map(canvas, null, 10, 10, extent.x, extent.y);
            e.x = rect.x;
            e.y = rect.y;
            e.width = rect.width;
            e.height = rect.height;
        }

        @Override
        public void getText(AccessibleTextEvent e) {
            int start = 0, end = text.length();
            switch (e.type) {
            case ACC.TEXT_BOUNDARY_ALL:
                start = e.start;
                end = e.end;
                break;
            case ACC.TEXT_BOUNDARY_CHAR:
                start = e.count >= 0 ? e.start + e.count : e.start - e.count;
                start = Math.max(0, Math.min(end, start));
                end = start;
                e.count = start - e.start;
                e.start = start;
                e.end = start;
                break;
            case ACC.TEXT_BOUNDARY_LINE:
                int offset = e.count <= 0 ? e.start : e.end;
                offset = Math.min(offset, text.length());
                int lineCount = 0;
                int index = 0;
                while (index != -1) {
                    lineCount++;
                    index = text.indexOf("\n", index);
                    if (index != -1)
                        index++;
                }
                e.count = e.count < 0 ? Math.max(e.count, -lineCount) : Math.min(e.count, lineCount);
                index = 0;
                int lastIndex = 0;
                String[] lines = new String[lineCount];
                for (int i = 0; i < lines.length; i++) {
                    index = text.indexOf("\n", index);
                    lines[i] = index != -1 ? text.substring(lastIndex, index) : text.substring(lastIndex);
                    lastIndex = index;
                    index++;
                }
                int len = 0;
                int lineAtOffset = 0;
                for (int i = 0; i < lines.length; i++) {
                    len += lines[i].length();
                    if (len >= e.offset) {
                        lineAtOffset = i;
                        break;
                    }
                }
                int result = Math.max(0, Math.min(lineCount - 1, lineAtOffset + e.count));
                e.count = result - lineAtOffset;
                e.result = lines[result];
                break;
            }
            e.result = text.substring(start, end);
        }

        @Override
        public void getSelectionCount(AccessibleTextEvent e) {
            e.count = 1;
        }

        @Override
        public void getSelection(AccessibleTextEvent e) {
            // there is only 1 selection, so index = 0
            getSelectionRange(e);
            e.start = e.offset;
            e.end = e.offset + e.length;
        }

        @Override
        public void getRanges(AccessibleTextEvent e) {
            // for now, ignore bounding box
            e.start = 0;
            e.end = text.length() - 1;
        }

        @Override
        public void getCharacterCount(AccessibleTextEvent e) {
            e.count = text.length();
        }

        @Override
        public void getVisibleRanges(AccessibleTextEvent e) {
            e.start = 0;
            e.end = text.length() - 1;
        }
    });
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

@SuppressWarnings("restriction")
public static void main(String[] args) {
    System.setProperty("swt.autoScale", "quarter");
    Display display = new Display();
    final Image eclipse = new Image(display, filenameProvider);
    final Image eclipseToolBar1 = new Image(display, filenameProvider);
    final Image eclipseToolBar2 = new Image(display, filenameProvider);
    final Image eclipseTableHeader = new Image(display, filenameProvider);
    final Image eclipseTableItem = new Image(display, filenameProvider);
    final Image eclipseTree1 = new Image(display, filenameProvider);
    final Image eclipseTree2 = new Image(display, filenameProvider);
    final Image eclipseCTab1 = new Image(display, filenameProvider);
    final Image eclipseCTab2 = new Image(display, filenameProvider);

    Shell shell = new Shell(display);
    shell.setText("Snippet 373");
    shell.setImage(eclipse);/* ww w.j  a  v a2  s. c o m*/
    shell.setText("DynamicDPI @ " + DPIUtil.getDeviceZoom());
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    shell.setLocation(100, 100);
    shell.setSize(500, 600);
    shell.addListener(SWT.ZoomChanged, new Listener() {
        @Override
        public void handleEvent(Event e) {
            if (display.getPrimaryMonitor().equals(shell.getMonitor())) {
                MessageBox box = new MessageBox(shell, SWT.PRIMARY_MODAL | SWT.OK | SWT.CANCEL);
                box.setText(shell.getText());
                box.setMessage("DPI changed, do you want to exit & restart ?");
                e.doit = (box.open() == SWT.OK);
                if (e.doit) {
                    shell.close();
                    System.out.println("Program exit.");
                }
            }
        }
    });

    // Menu
    Menu bar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(bar);
    MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
    fileItem.setText("&File");
    fileItem.setImage(eclipse);
    Menu submenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(submenu);
    MenuItem subItem = new MenuItem(submenu, SWT.PUSH);
    subItem.addListener(SWT.Selection, e -> System.out.println("Select All"));
    subItem.setText("Select &All\tCtrl+A");
    subItem.setAccelerator(SWT.MOD1 + 'A');
    subItem.setImage(eclipse);

    // CTabFolder
    CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 2; i++) {
        CTabItem cTabItem = new CTabItem(folder, i % 2 == 0 ? SWT.CLOSE : SWT.NONE);
        cTabItem.setText("Item " + i);
        Text textMsg = new Text(folder, SWT.MULTI);
        textMsg.setText("Content for Item " + i);
        cTabItem.setControl(textMsg);
        cTabItem.setImage((i % 2 == 1) ? eclipseCTab1 : eclipseCTab2);
    }

    // PerMonitorV2 setting
    //      Label label = new Label (shell, SWT.BORDER);
    //      label.setText("PerMonitorV2 value before:after:Error");
    //      Text text = new Text(shell, SWT.BORDER);
    //      text.setText(DPIUtil.BEFORE + ":" + DPIUtil.AFTER + ":" + DPIUtil.RESULT);

    // Composite for Label, Button, Tool-bar
    Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new RowLayout(SWT.HORIZONTAL));

    // Label with Image
    Label label1 = new Label(composite, SWT.BORDER);
    label1.setImage(eclipse);

    // Label with text only
    Label label2 = new Label(composite, SWT.BORDER);
    label2.setText("No Image");

    // Button with text + Old Image Constructor
    Button oldButton1 = new Button(composite, SWT.PUSH);
    oldButton1.setText("Old Img");
    oldButton1.setImage(new Image(display, IMAGE_PATH_100));

    // Button with Old Image Constructor
    //      Button oldButton2 = new Button(composite, SWT.PUSH);
    //      oldButton2.setImage(new Image(display, filenameProvider.getImagePath(100)));

    // Button with Image
    Button createDialog = new Button(composite, SWT.PUSH);
    createDialog.setText("Child Dialog");
    createDialog.setImage(eclipse);
    createDialog.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.RESIZE);
            dialog.setText("Child Dialog");
            RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
            dialog.setLayout(rowLayout);
            Label label = new Label(dialog, SWT.BORDER);
            label.setImage(eclipse);
            Point location = shell.getLocation();
            dialog.setLocation(location.x + 250, location.y + 50);
            dialog.pack();
            dialog.open();
        }
    });

    // Toolbar with Image
    ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.BORDER);
    Rectangle clientArea = shell.getClientArea();
    toolBar.setLocation(clientArea.x, clientArea.y);
    for (int i = 0; i < 2; i++) {
        int style = i % 2 == 1 ? SWT.DROP_DOWN : SWT.PUSH;
        ToolItem toolItem = new ToolItem(toolBar, style);
        toolItem.setImage((i % 2 == 0) ? eclipseToolBar1 : eclipseToolBar2);
        toolItem.setEnabled(i % 2 == 0);
    }
    toolBar.pack();

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Refresh-Current Monitor : Zoom");
    Text text1 = new Text(shell, SWT.BORDER);
    Monitor monitor = button.getMonitor();
    text1.setText("" + monitor.getZoom());
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent e) {
            Monitor monitor = button.getMonitor();
            text1.setText("" + monitor.getZoom());
        }
    });
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Refresh-Both Monitors : Zoom");
    Text text2 = new Text(shell, SWT.BORDER);
    Monitor[] monitors = display.getMonitors();
    StringBuilder text2String = new StringBuilder();
    for (int i = 0; i < monitors.length; i++) {
        text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : ""));
    }
    text2.setText(text2String.toString());
    button2.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent e) {
            Monitor[] monitors = display.getMonitors();
            StringBuilder text2String = new StringBuilder();
            for (int i = 0; i < monitors.length; i++) {
                text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : ""));
            }
            text2.setText(text2String.toString());
        }
    });

    // Table
    Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    String titles[] = { "Title 1" };
    for (int i = 0; i < titles.length; i++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(titles[i]);
        column.setImage(eclipseTableHeader);
    }
    for (int i = 0; i < 1; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(0, "Data " + i);
        item.setImage(0, eclipseTableItem);
    }
    for (int i = 0; i < titles.length; i++) {
        table.getColumn(i).pack();
    }

    // Tree
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 1; i++) {
        TreeItem iItem = new TreeItem(tree, 0);
        iItem.setText("TreeItem (0) -" + i);
        iItem.setImage(eclipseTree1);
        TreeItem jItem = null;
        for (int j = 0; j < 1; j++) {
            jItem = new TreeItem(iItem, 0);
            jItem.setText("TreeItem (1) -" + j);
            jItem.setImage(eclipseTree2);
            jItem.setExpanded(true);
        }
        tree.select(jItem);
    }

    // Shell Location
    Monitor primary = display.getPrimaryMonitor();
    Rectangle bounds = primary.getBounds();
    Rectangle rect = shell.getBounds();
    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;
    shell.setLocation(x, y);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing/AWT Example");

    Listener exitListener = e -> {//from   w w  w . j a  v a2  s . c o m
        MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
        dialog.setText("Question");
        dialog.setMessage("Exit?");
        if (e.type == SWT.Close)
            e.doit = false;
        if (dialog.open() != SWT.OK)
            return;
        shell.dispose();
    };
    Listener aboutListener = e -> {
        final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        s.setText("About");
        GridLayout layout = new GridLayout(1, false);
        layout.verticalSpacing = 20;
        layout.marginHeight = layout.marginWidth = 10;
        s.setLayout(layout);
        Label label = new Label(s, SWT.NONE);
        label.setText("SWT and AWT Example.");
        Button button = new Button(s, SWT.PUSH);
        button.setText("OK");
        GridData data = new GridData();
        data.horizontalAlignment = GridData.CENTER;
        button.setLayoutData(data);
        button.addListener(SWT.Selection, event -> s.dispose());
        s.pack();
        Rectangle parentBounds = shell.getBounds();
        Rectangle bounds = s.getBounds();
        int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
        int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
        s.setLocation(x, y);
        s.open();
        while (!s.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    };
    shell.addListener(SWT.Close, exitListener);
    Menu mb = new Menu(shell, SWT.BAR);
    MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit\tCtrl+X");
    exitItem.setAccelerator(SWT.CONTROL + 'X');
    exitItem.addListener(SWT.Selection, exitListener);
    MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
    aboutItem.setText("&About\tCtrl+A");
    aboutItem.setAccelerator(SWT.CONTROL + 'A');
    aboutItem.addListener(SWT.Selection, aboutListener);
    shell.setMenuBar(mb);

    RGB color = shell.getBackground().getRGB();
    Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Label locationLb = new Label(shell, SWT.NONE);
    locationLb.setText("Location:");
    Composite locationComp = new Composite(shell, SWT.EMBEDDED);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
    ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
    exitToolItem.setText("&Exit");
    exitToolItem.addListener(SWT.Selection, exitListener);
    ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
    aboutToolItem.setText("&About");
    aboutToolItem.addListener(SWT.Selection, aboutListener);
    Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    final Composite comp = new Composite(shell, SWT.NONE);
    final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
    Sash sash = new Sash(comp, SWT.VERTICAL);
    Composite tableComp = new Composite(comp, SWT.EMBEDDED);
    Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Composite statusComp = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
    final java.awt.TextField locationText = new java.awt.TextField();
    locationFrame.add(locationText);

    java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
    fileTableFrame.add(panel);
    final JTable fileTable = new JTable(new FileTableModel(null));
    fileTable.setDoubleBuffered(true);
    fileTable.setShowGrid(false);
    fileTable.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(fileTable);
    panel.add(scrollPane);

    java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
    statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
    final java.awt.Label statusLabel = new java.awt.Label();
    statusFrame.add(statusLabel);
    statusLabel.setText("Select a file");

    sash.addListener(SWT.Selection, e -> {
        if (e.detail == SWT.DRAG)
            return;
        GridData data = (GridData) fileTree.getLayoutData();
        Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
        data.widthHint = e.x - trim.width;
        comp.layout();
    });

    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
        treeItem.setText(file.getAbsolutePath());
        treeItem.setData(file);
        new TreeItem(treeItem, SWT.NONE);
    }
    fileTree.addListener(SWT.Expand, e -> {
        TreeItem item = (TreeItem) e.item;
        if (item == null)
            return;
        if (item.getItemCount() == 1) {
            TreeItem firstItem = item.getItems()[0];
            if (firstItem.getData() != null)
                return;
            firstItem.dispose();
        } else {
            return;
        }
        File root = (File) item.getData();
        File[] files = root.listFiles();
        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                TreeItem treeItem = new TreeItem(item, SWT.NONE);
                treeItem.setText(file.getName());
                treeItem.setData(file);
                new TreeItem(treeItem, SWT.NONE);
            }
        }
    });
    fileTree.addListener(SWT.Selection, e -> {
        TreeItem item = (TreeItem) e.item;
        if (item == null)
            return;
        final File root = (File) item.getData();
        EventQueue.invokeLater(() -> {
            statusLabel.setText(root.getAbsolutePath());
            locationText.setText(root.getAbsolutePath());
            fileTable.setModel(new FileTableModel(root.listFiles()));
        });
    });

    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    shell.setLayout(layout);
    GridData data;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator1.setLayoutData(data);
    data = new GridData();
    data.horizontalSpan = 1;
    data.horizontalIndent = 10;
    locationLb.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    data.heightHint = locationText.getPreferredSize().height;
    locationComp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 1;
    toolBar.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator2.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 4;
    comp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator3.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    data.heightHint = statusLabel.getPreferredSize().height;
    statusComp.setLayoutData(data);

    layout = new GridLayout(3, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    comp.setLayout(layout);
    data = new GridData(GridData.FILL_VERTICAL);
    data.widthHint = 200;
    fileTree.setLayoutData(data);
    data = new GridData(GridData.FILL_VERTICAL);
    sash.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    tableComp.setLayoutData(data);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.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  www.  j av a  2  s  . c  o 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();
}

From source file:Snippet135.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing/AWT Example");

    Listener exitListener = new Listener() {
        public void handleEvent(Event e) {
            MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
            dialog.setText("Question");
            dialog.setMessage("Exit?");
            if (e.type == SWT.Close)
                e.doit = false;//from   w w  w  .  j av  a2s .c  o m
            if (dialog.open() != SWT.OK)
                return;
            shell.dispose();
        }
    };
    Listener aboutListener = new Listener() {
        public void handleEvent(Event e) {
            final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
            s.setText("About");
            GridLayout layout = new GridLayout(1, false);
            layout.verticalSpacing = 20;
            layout.marginHeight = layout.marginWidth = 10;
            s.setLayout(layout);
            Label label = new Label(s, SWT.NONE);
            label.setText("SWT and AWT Example.");
            Button button = new Button(s, SWT.PUSH);
            button.setText("OK");
            GridData data = new GridData();
            data.horizontalAlignment = GridData.CENTER;
            button.setLayoutData(data);
            button.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    s.dispose();
                }
            });
            s.pack();
            Rectangle parentBounds = shell.getBounds();
            Rectangle bounds = s.getBounds();
            int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
            int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
            s.setLocation(x, y);
            s.open();
            while (!s.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }
    };
    shell.addListener(SWT.Close, exitListener);
    Menu mb = new Menu(shell, SWT.BAR);
    MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit\tCtrl+X");
    exitItem.setAccelerator(SWT.CONTROL + 'X');
    exitItem.addListener(SWT.Selection, exitListener);
    MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
    aboutItem.setText("&About\tCtrl+A");
    aboutItem.setAccelerator(SWT.CONTROL + 'A');
    aboutItem.addListener(SWT.Selection, aboutListener);
    shell.setMenuBar(mb);

    RGB color = shell.getBackground().getRGB();
    Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Label locationLb = new Label(shell, SWT.NONE);
    locationLb.setText("Location:");
    Composite locationComp = new Composite(shell, SWT.EMBEDDED);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
    ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
    exitToolItem.setText("&Exit");
    exitToolItem.addListener(SWT.Selection, exitListener);
    ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
    aboutToolItem.setText("&About");
    aboutToolItem.addListener(SWT.Selection, aboutListener);
    Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    final Composite comp = new Composite(shell, SWT.NONE);
    final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
    Sash sash = new Sash(comp, SWT.VERTICAL);
    Composite tableComp = new Composite(comp, SWT.EMBEDDED);
    Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Composite statusComp = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
    final java.awt.TextField locationText = new java.awt.TextField();
    locationFrame.add(locationText);

    java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
    fileTableFrame.add(panel);
    final JTable fileTable = new JTable(new FileTableModel(null));
    fileTable.setDoubleBuffered(true);
    fileTable.setShowGrid(false);
    fileTable.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(fileTable);
    panel.add(scrollPane);

    java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
    statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
    final java.awt.Label statusLabel = new java.awt.Label();
    statusFrame.add(statusLabel);
    statusLabel.setText("Select a file");

    sash.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            if (e.detail == SWT.DRAG)
                return;
            GridData data = (GridData) fileTree.getLayoutData();
            Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
            data.widthHint = e.x - trim.width;
            comp.layout();
        }
    });

    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
        treeItem.setText(file.getAbsolutePath());
        treeItem.setData(file);
        new TreeItem(treeItem, SWT.NONE);
    }
    fileTree.addListener(SWT.Expand, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            if (item.getItemCount() == 1) {
                TreeItem firstItem = item.getItems()[0];
                if (firstItem.getData() != null)
                    return;
                firstItem.dispose();
            } else {
                return;
            }
            File root = (File) item.getData();
            File[] files = root.listFiles();
            if (files == null)
                return;
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    TreeItem treeItem = new TreeItem(item, SWT.NONE);
                    treeItem.setText(file.getName());
                    treeItem.setData(file);
                    new TreeItem(treeItem, SWT.NONE);
                }
            }
        }
    });
    fileTree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            final File root = (File) item.getData();
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    statusLabel.setText(root.getAbsolutePath());
                    locationText.setText(root.getAbsolutePath());
                    fileTable.setModel(new FileTableModel(root.listFiles()));
                }
            });
        }
    });

    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    shell.setLayout(layout);
    GridData data;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator1.setLayoutData(data);
    data = new GridData();
    data.horizontalSpan = 1;
    data.horizontalIndent = 10;
    locationLb.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    data.heightHint = locationText.getPreferredSize().height;
    locationComp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 1;
    toolBar.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator2.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 4;
    comp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator3.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    data.heightHint = statusLabel.getPreferredSize().height;
    statusComp.setLayoutData(data);

    layout = new GridLayout(3, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    comp.setLayout(layout);
    data = new GridData(GridData.FILL_VERTICAL);
    data.widthHint = 200;
    fileTree.setLayoutData(data);
    data = new GridData(GridData.FILL_VERTICAL);
    sash.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    tableComp.setLayoutData(data);

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet365 - Transparent Background");
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.spacing = 20;/*from w w w .  j  ava2  s .com*/
    layout.marginWidth = 10;
    layout.marginHeight = 10;
    shell.setLayout(layout);
    // Standard color background for Shell
    // shell.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // Gradient background for Shell
    shell.addListener(SWT.Resize, 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_BLUE));
        gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
        gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
        gc.dispose();
        shell.setBackgroundImage(newImage);
        if (oldImage != null)
            oldImage.dispose();
        oldImage = newImage;
    });

    // Transparent
    buttonCheckBox = new Button(shell, SWT.CHECK | SWT.None);
    buttonCheckBox.setText("SET TRANSPARENT");
    buttonCheckBox.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
    buttonCheckBox.setSelection(false);
    buttonCheckBox.addSelectionListener(widgetSelectedAdapter(e -> {
        boolean transparent = ((Button) e.getSource()).getSelection();
        if (transparent) {
            // ContainerGroup
            containerGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            canvas.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            composite.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            tabFolder.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            for (TabItem item : tabFolder.getItems()) {
                item.getControl().setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            }

            // Native
            nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            toolBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            coolBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            label.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            link.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            scale.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            radio.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            check.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            group.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            sash.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            slider.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            list.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));

            // Custom
            customGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            cLabel.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            cTab.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT));
            gradientCTab.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT));
            sashForm.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT));
            for (Control control : sashForm.getChildren()) {
                control.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT));
            }
            // Default
            push.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            combo.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            progressBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            dateTime.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            ccombo.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            text.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            styledText.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            expandBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            table.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            tree.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));

            // ItemGroup
            itemGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
        } else {
            // Native
            nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            toolBar.setBackground(null);
            coolBar.setBackground(null);
            label.setBackground(null);
            link.setBackground(null);
            scale.setBackground(null);
            RGB rgb = display.getSystemColor(SWT.COLOR_CYAN).getRGB();
            radio.setBackground(new Color(display, new RGBA(rgb.red, rgb.blue, rgb.green, 255)));
            check.setBackgroundImage(getBackgroundImage(display));
            group.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            sash.setBackground(display.getSystemColor(SWT.COLOR_DARK_CYAN));
            slider.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            list.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            text.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

            // ContainerGroup
            containerGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            canvas.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            composite.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            tabFolder.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            for (TabItem item : tabFolder.getItems()) {
                item.getControl().setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            }
            // Custom
            customGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            cLabel.setBackground((Color) null);
            styledText.setBackground((Color) null);
            sashForm.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            for (Control control : sashForm.getChildren()) {
                control.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            }
            cTab.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

            gradientCTab.setBackground(new Color[] { display.getSystemColor(SWT.COLOR_RED),
                    display.getSystemColor(SWT.COLOR_WHITE) }, new int[] { 90 }, true);

            // Default
            defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            push.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            combo.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            ccombo.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            dateTime.setBackground(null);
            progressBar.setBackground(null);
            expandBar.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            table.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            tree.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

            // ItemGroup
            itemGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        }

    }));

    // ContainerGroup
    containerGroup = new Composite(shell, SWT.NONE);
    containerGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    containerGroup.setToolTipText("CONTAINER");
    layout = new RowLayout();
    layout.spacing = 20;
    containerGroup.setLayout(layout);

    // Native
    nativeGroup = new Composite(shell, SWT.NONE);
    nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    nativeGroup.setToolTipText("NATIVE");
    layout = new RowLayout();
    layout.spacing = 20;
    nativeGroup.setLayout(layout);

    // Custom
    customGroup = new Composite(shell, SWT.NONE);
    customGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    customGroup.setToolTipText("CUSTOM");
    layout = new RowLayout();
    layout.spacing = 20;
    customGroup.setLayout(layout);

    // AsDesigned
    defaultBackgroundGroup = new Composite(shell, SWT.NONE);
    defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    defaultBackgroundGroup.setToolTipText("Default Background");
    layout = new RowLayout();
    layout.spacing = 20;
    defaultBackgroundGroup.setLayout(layout);

    // ItemGroup
    itemGroup = new Composite(shell, SWT.NONE);
    itemGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    itemGroup.setToolTipText("ITEM");
    layout = new RowLayout();
    layout.spacing = 20;
    itemGroup.setLayout(layout);

    // Label
    label = new Label(nativeGroup, SWT.NONE);
    label.setText("Label");

    // Radio button
    radio = new Button(nativeGroup, SWT.RADIO);
    radio.setText("Radio Button");
    radio.setSelection(true);
    radio.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // Checkbox button with image
    check = new Button(nativeGroup, SWT.CHECK);
    check.setText("CheckBox Image");
    check.setSelection(true);
    check.setBackgroundImage(getBackgroundImage(display));

    // Push Button
    push = new Button(defaultBackgroundGroup, SWT.PUSH);
    push.setText("Push Button");

    // Toolbar
    toolBar = new ToolBar(nativeGroup, SWT.FLAT);
    toolBar.pack();
    ToolItem item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("ToolBar_Item");

    // Coolbar
    coolBar = new CoolBar(nativeGroup, SWT.BORDER);
    for (int i = 0; i < 2; i++) {
        CoolItem item2 = new CoolItem(coolBar, SWT.NONE);
        Button button = new Button(coolBar, SWT.PUSH);
        button.setText("Button " + i);
        Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        item2.setPreferredSize(item2.computeSize(size.x, size.y));
        item2.setControl(button);
    }
    // Scale
    scale = new Scale(nativeGroup, SWT.None);
    scale.setMaximum(100);
    scale.setSelection(20);

    // Link
    link = new Link(nativeGroup, SWT.NONE);
    link.setText("<a>Sample link</a>");

    // List
    list = new List(nativeGroup, SWT.BORDER);
    list.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    list.add("List_one");
    list.add("List_two");
    list.add("List_three");
    list.add("List_four");

    // Canvas
    canvas = new Canvas(containerGroup, SWT.NONE);
    canvas.setToolTipText("Canvas");
    canvas.addPaintListener(e -> {
        GC gc = e.gc;
        gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
        gc.drawRectangle(e.x + 1, e.y + 1, e.width - 2, e.height - 2);
        gc.drawArc(2, 2, e.width - 4, e.height - 4, 0, 360);
    });

    // Composite
    composite = new Composite(containerGroup, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    composite.setToolTipText("Composite");

    // TabFolder
    tabFolder = new TabFolder(containerGroup, SWT.BORDER);
    tabFolder.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    for (int i = 0; i < 2; i++) {
        TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
        tabItem.setText("TabItem " + i);
        Label label = new Label(tabFolder, SWT.PUSH);
        label.setText("Page " + i);
        tabItem.setControl(label);
        tabItem.getControl().setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    }
    tabFolder.pack();

    // Group
    group = new Group(containerGroup, SWT.NONE);
    group.setText("Group");

    // Sash
    sash = new Sash(containerGroup, SWT.HORIZONTAL | SWT.BORDER);
    sash.setBackground(display.getSystemColor(SWT.COLOR_DARK_CYAN));
    sash.setLayoutData(new RowData(100, 100));
    sash.setToolTipText("Sash");

    // SashForm
    sashForm = new SashForm(containerGroup, SWT.HORIZONTAL | SWT.BORDER);
    Label leftLabel = new Label(sashForm, SWT.NONE);
    leftLabel.setText("SashForm\nLeft\n...\n...\n...\n...\n...");
    Label rightLabel = new Label(sashForm, SWT.NONE);
    rightLabel.setText("SashForm\nRight\n...\n...\n...\n...\n...");

    // DateTime
    dateTime = new DateTime(defaultBackgroundGroup, SWT.NONE);
    dateTime.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // Text
    text = new Text(nativeGroup, SWT.BORDER);
    text.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    text.setText("text");

    // ProgressBar
    progressBar = new ProgressBar(defaultBackgroundGroup, SWT.NONE);
    progressBar.setMaximum(100);
    progressBar.setSelection(80);

    // Combo
    combo = new Combo(defaultBackgroundGroup, SWT.BORDER);
    combo.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    combo.add("combo");
    combo.setText("combo");

    // Slider
    slider = new Slider(nativeGroup, SWT.HORIZONTAL | SWT.BORDER);
    slider.setSelection(20);
    slider.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // CCombo
    ccombo = new CCombo(defaultBackgroundGroup, SWT.BORDER);
    ccombo.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    ccombo.add("ccombo");
    ccombo.setText("ccombo");

    // CLable
    cLabel = new CLabel(customGroup, SWT.NONE);
    cLabel.setText("CLabel");

    // Text
    styledText = new StyledText(customGroup, SWT.BORDER);
    styledText.setFont(new Font(display, "Tahoma", 18, SWT.BOLD | SWT.ITALIC));
    styledText.setForeground(display.getSystemColor(SWT.COLOR_DARK_BLUE));
    styledText.setText("Styled Text");
    styledText.append("\n");
    styledText.append("Example_string");
    styledText.append("\n");
    styledText.append("One_Two");
    styledText.append("\n");
    styledText.append("Two_Three");

    // CTabFolder
    cTab = new CTabFolder(containerGroup, SWT.BORDER);
    for (int i = 0; i < 2; i++) {
        CTabItem cTabItem = new CTabItem(cTab, SWT.CLOSE, i);
        cTabItem.setText("CTabItem " + (i + 1));
    }
    cTab.setSelection(0);

    // Gradient CTabFolder
    gradientCTab = new CTabFolder(customGroup, SWT.BORDER);
    gradientCTab.setBackground(
            new Color[] { display.getSystemColor(SWT.COLOR_WHITE), display.getSystemColor(SWT.COLOR_RED) },
            new int[] { 90 }, true);
    for (int i = 0; i < 2; i++) {
        CTabItem cTabItem = new CTabItem(gradientCTab, SWT.CLOSE, i);
        cTabItem.setText("CTabItem " + (i + 1));
    }
    gradientCTab.setSelection(0);

    // Table
    table = new Table(itemGroup, SWT.V_SCROLL);
    table.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    TableItem tableItem = new TableItem(table, SWT.NONE);
    tableItem.setText("TableItem - One");
    tableItem = new TableItem(table, SWT.NONE);
    tableItem.setText("TableItem - Two");

    // Tree
    tree = new Tree(itemGroup, SWT.NONE);
    TreeItem treeItem = new TreeItem(tree, SWT.NONE);
    treeItem.setText("Parent");
    TreeItem childItem = new TreeItem(treeItem, SWT.NONE);
    childItem.setText("Child1");
    childItem = new TreeItem(treeItem, SWT.NONE);
    childItem.setText("Child2");
    treeItem.setExpanded(true);
    tree.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // ExpandBar
    expandBar = new ExpandBar(itemGroup, SWT.V_SCROLL);
    expandBar.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    for (int i = 1; i <= 2; i++) {
        ExpandItem item1 = new ExpandItem(expandBar, SWT.NONE, 0);
        item1.setText("Expand_Bar_Entry " + i);
        item1.setExpanded(true);
        item1.setHeight(20);
    }

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

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

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 336");
    shell.setLayout(new GridLayout());
    TabFolder folder = new TabFolder(shell, SWT.NONE);
    folder.setLayoutData(new GridData(GridData.FILL_BOTH));

    //Progress tab
    TabItem item = new TabItem(folder, SWT.NONE);
    item.setText("Progress");
    Composite composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);//w  w w  . j a v  a  2s  .c  om
    Listener listener = event -> {
        Button button = (Button) event.widget;
        if (!button.getSelection())
            return;
        TaskItem item1 = getTaskBarItem();
        if (item1 != null) {
            int state = ((Integer) button.getData()).intValue();
            item1.setProgressState(state);
        }
    };
    Group group = new Group(composite, SWT.NONE);
    group.setText("State");
    group.setLayout(new GridLayout());
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Button button;
    String[] stateLabels = { "SWT.DEFAULT", "SWT.INDETERMINATE", "SWT.NORMAL", "SWT.ERROR", "SWT.PAUSED" };
    int[] states = { SWT.DEFAULT, SWT.INDETERMINATE, SWT.NORMAL, SWT.ERROR, SWT.PAUSED };
    for (int i = 0; i < states.length; i++) {
        button = new Button(group, SWT.RADIO);
        button.setText(stateLabels[i]);
        button.setData(Integer.valueOf(states[i]));
        button.addListener(SWT.Selection, listener);
        if (i == 0)
            button.setSelection(true);
    }
    group = new Group(composite, SWT.NONE);
    group.setText("Value");
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Label label = new Label(group, SWT.NONE);
    label.setText("Progress");
    final Scale scale = new Scale(group, SWT.NONE);
    scale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    scale.addListener(SWT.Selection, event -> {
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setProgress(scale.getSelection());
    });

    //Overlay text tab
    item = new TabItem(folder, SWT.NONE);
    item.setText("Text");
    composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);
    group = new Group(composite, SWT.NONE);
    group.setText("Enter a short text:");
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    final Text text = new Text(group, SWT.BORDER | SWT.SINGLE);
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    text.setLayoutData(data);
    button = new Button(group, SWT.PUSH);
    button.setText("Set");
    button.addListener(SWT.Selection, event -> {
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setOverlayText(text.getText());
    });
    button = new Button(group, SWT.PUSH);
    button.setText("Clear");
    button.addListener(SWT.Selection, event -> {
        text.setText("");
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setOverlayText("");
    });

    //Overlay image tab
    item = new TabItem(folder, SWT.NONE);
    item.setText("Image");
    composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);
    Listener listener3 = event -> {
        Button button1 = (Button) event.widget;
        if (!button1.getSelection())
            return;
        TaskItem item1 = getTaskBarItem();
        if (item1 != null) {
            String text1 = button1.getText();
            Image image = null;
            if (!text1.equals("NONE"))
                image = new Image(display, Snippet336.class.getResourceAsStream(text1));
            Image oldImage = item1.getOverlayImage();
            item1.setOverlayImage(image);
            if (oldImage != null)
                oldImage.dispose();
        }
    };
    group = new Group(composite, SWT.NONE);
    group.setText("Images");
    group.setLayout(new GridLayout());
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    button = new Button(group, SWT.RADIO);
    button.setText("NONE");
    button.addListener(SWT.Selection, listener3);
    button.setSelection(true);
    String[] images = { "eclipse.png", "pause.gif", "run.gif", "warning.gif" };
    for (int i = 0; i < images.length; i++) {
        button = new Button(group, SWT.RADIO);
        button.setText(images[i]);
        button.addListener(SWT.Selection, listener3);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:GridLayoutDialog.java

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

    Label labelUser;/*from w w w. jav  a2 s  .  com*/
    Label labelFile;

    final Text textUser;
    final Text textFile;

    Button buttonBrowseFile;
    Button buttonUpload;

    GridLayout gridLayout = new GridLayout(3, false);
    shell.setLayout(gridLayout);

    labelUser = new Label(shell, SWT.NULL);

    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.grabExcessHorizontalSpace = true;
    textUser = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textUser.setLayoutData(gridData);

    new Label(shell, SWT.NULL);

    // 2nd row.
    labelFile = new Label(shell, SWT.NULL);

    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.grabExcessHorizontalSpace = true;
    textFile = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textFile.setLayoutData(gridData);

    buttonBrowseFile = new Button(shell, SWT.PUSH);

    // last row.
    gridData = new GridData();
    gridData.horizontalSpan = 3;
    gridData.horizontalAlignment = GridData.CENTER;
    buttonUpload = new Button(shell, SWT.PUSH);
    buttonUpload.setLayoutData(gridData);

    labelUser.setText("User name: ");
    labelFile.setText("Photo: ");

    buttonBrowseFile.setText("Browse");
    buttonUpload.setText("Upload");

    buttonBrowseFile.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            FileDialog dialog = new FileDialog(shell, SWT.OPEN);
            String file = dialog.open();
            if (file != null) {
                textFile.setText(file);
            }
        }
    });

    buttonUpload.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println(textUser.getText());
            System.out.println(textFile.getText());
            shell.dispose();
        }
    });

    shell.pack();
    shell.open();
    textUser.forceFocus();

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

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

static void createChildren(Composite parent) {
    parent.setLayout(new RowLayout());
    List list = new List(parent, SWT.BORDER | SWT.MULTI);
    list.add("List item 1");
    list.add("List item 2");
    Label label = new Label(parent, SWT.NONE);
    label.setText("Label");
    Button button = new Button(parent, SWT.RADIO);
    button.setText("Radio Button");
    button = new Button(parent, SWT.CHECK);
    button.setText("Check box Button");
    button = new Button(parent, SWT.PUSH);
    button.setText("Push Button");
    Text text = new Text(parent, SWT.BORDER);
    text.setText("Text");
}

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 ww  w.j a va2s .  c  om*/
    });
    return button;
}