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

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

Introduction

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

Prototype

public void setText(String text) 

Source Link

Document

Sets the receiver's text.

Usage

From source file:ComboSelectedIndex.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Show Message Box");
    shell.setLayout(new GridLayout(2, false));
    new Label(shell, SWT.NONE).setText("Icon:");
    final Combo icons = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    icons.add("A");
    icons.add("B");
    icons.add("C");
    icons.select(0);/*from  w ww  .j  a v  a 2 s .  c  om*/

    new Label(shell, SWT.NONE).setText("Buttons:");
    final Combo buttons = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    buttons.add("1");
    buttons.add("2");
    buttons.select(0);

    new Label(shell, SWT.NONE).setText("Return:");
    final Label returnVal = new Label(shell, SWT.NONE);
    returnVal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Show Message");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {

            returnVal.setText("");

            returnVal.setText(icons.getSelectionIndex() + " " + buttons.getSelectionIndex());
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 171");
    shell.setLayout(new FillLayout());
    final Label label1 = new Label(shell, SWT.BORDER | SWT.WRAP);
    label1.setText("Drag Source for MyData and MyData2");
    final Label label2 = new Label(shell, SWT.BORDER | SWT.WRAP);
    label2.setText("Drop Target for MyData");
    final Label label3 = new Label(shell, SWT.BORDER | SWT.WRAP);
    label3.setText("Drop Target for MyData2");

    DragSource source = new DragSource(label1, DND.DROP_COPY);
    source.setTransfer(MyTransfer.getInstance(), MyTransfer2.getInstance());
    source.addDragListener(new DragSourceAdapter() {
        @Override/*w ww . j  a va 2  s .  c om*/
        public void dragSetData(DragSourceEvent event) {
            MyType2 myType = new MyType2();
            myType.fileName = "abc.txt";
            myType.fileLength = 1000;
            myType.lastModified = 12312313;
            myType.version = "version 2";
            event.data = myType;
        }
    });
    DropTarget targetMyType = new DropTarget(label2, DND.DROP_COPY | DND.DROP_DEFAULT);
    targetMyType.setTransfer(MyTransfer.getInstance());
    targetMyType.addDropListener(new DropTargetAdapter() {
        @Override
        public void dragEnter(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT)
                event.detail = DND.DROP_COPY;
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT)
                event.detail = DND.DROP_COPY;
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data != null) {
                MyType myType = (MyType) event.data;
                if (myType != null) {
                    String string = "MyType: " + myType.fileName;
                    label2.setText(string);
                }
            }
        }

    });
    DropTarget targetMyType2 = new DropTarget(label3, DND.DROP_COPY | DND.DROP_DEFAULT);
    targetMyType2.setTransfer(MyTransfer2.getInstance());
    targetMyType2.addDropListener(new DropTargetAdapter() {
        @Override
        public void dragEnter(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT)
                event.detail = DND.DROP_COPY;
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT)
                event.detail = DND.DROP_COPY;
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data != null) {
                MyType2 myType = (MyType2) event.data;
                if (myType != null) {
                    String string = "MyType2: " + myType.fileName + ":" + myType.version;
                    label3.setText(string);
                }
            }
        }

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

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 -> {/* w w  w . j  av  a 2s .c  om*/
        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.Snippet208.java

public static void main(String[] args) {
    PaletteData palette = new PaletteData(0xff, 0xff00, 0xff0000);

    // ImageData showing variations of hue
    ImageData hueData = new ImageData(360, 100, 24, palette);
    float hue = 0;
    for (int x = 0; x < hueData.width; x++) {
        for (int y = 0; y < hueData.height; y++) {
            int pixel = palette.getPixel(new RGB(hue, 1f, 1f));
            hueData.setPixel(x, y, pixel);
        }//from ww  w . jav a  2s . co m
        hue += 360f / hueData.width;
    }

    // ImageData showing saturation on x axis and brightness on y axis
    ImageData saturationBrightnessData = new ImageData(360, 360, 24, palette);
    float saturation = 0f;
    float brightness = 1f;
    for (int x = 0; x < saturationBrightnessData.width; x++) {
        brightness = 1f;
        for (int y = 0; y < saturationBrightnessData.height; y++) {
            int pixel = palette.getPixel(new RGB(360f, saturation, brightness));
            saturationBrightnessData.setPixel(x, y, pixel);
            brightness -= 1f / saturationBrightnessData.height;
        }
        saturation += 1f / saturationBrightnessData.width;
    }

    Display display = new Display();
    Image hueImage = new Image(display, hueData);
    Image saturationImage = new Image(display, saturationBrightnessData);
    Shell shell = new Shell(display);
    shell.setText("Hue, Saturation, Brightness");
    GridLayout gridLayout = new GridLayout(2, false);
    gridLayout.verticalSpacing = 10;
    gridLayout.marginWidth = gridLayout.marginHeight = 16;
    shell.setLayout(gridLayout);

    Label label = new Label(shell, SWT.CENTER);
    label.setImage(hueImage);
    GridData data = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 2, 1);
    label.setLayoutData(data);

    label = new Label(shell, SWT.CENTER); //spacer
    label = new Label(shell, SWT.CENTER);
    label.setText("Hue");
    data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
    label.setLayoutData(data);
    label = new Label(shell, SWT.CENTER); //spacer
    data = new GridData(SWT.CENTER, SWT.CENTER, false, false, 2, 1);
    label.setLayoutData(data);

    label = new Label(shell, SWT.LEFT);
    label.setText("Brightness");
    data = new GridData(SWT.LEFT, SWT.CENTER, false, false);
    label.setLayoutData(data);

    label = new Label(shell, SWT.CENTER);
    label.setImage(saturationImage);
    data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
    label.setLayoutData(data);

    label = new Label(shell, SWT.CENTER); //spacer
    label = new Label(shell, SWT.CENTER);
    label.setText("Saturation");
    data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
    label.setLayoutData(data);

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    hueImage.dispose();
    saturationImage.dispose();
    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);/*from   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 ww  . j a  v  a  2 s.com*/
        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: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;// w  w  w .  j a  va 2s .co 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.Snippet153.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 153");
    shell.setBounds(10, 10, 200, 200);/*  w  w  w.  j a v a  2  s  .c  o m*/
    final ToolBar bar = new ToolBar(shell, SWT.BORDER);
    bar.setBounds(10, 10, 150, 50);
    final Label statusLine = new Label(shell, SWT.BORDER);
    statusLine.setBounds(10, 90, 150, 30);
    new ToolItem(bar, SWT.NONE).setText("item 1");
    new ToolItem(bar, SWT.NONE).setText("item 2");
    new ToolItem(bar, SWT.NONE).setText("item 3");
    bar.addMouseMoveListener(e -> {
        ToolItem item = bar.getItem(new Point(e.x, e.y));
        String name = "";
        if (item != null) {
            name = item.getText();
        }
        if (!statusText.equals(name)) {
            statusLine.setText(name);
            statusText = name;
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

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

    Canvas blankCanvas = new Canvas(shell, SWT.BORDER);
    blankCanvas.setLayoutData(new GridData(200, 200));
    final Label statusLine = new Label(shell, SWT.NONE);
    statusLine.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    Menu bar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(bar);//from  w w w .  j a v a  2s .c  o m

    MenuItem menuItem = new MenuItem(bar, SWT.CASCADE);
    menuItem.setText("Test");
    Menu menu = new Menu(bar);
    menuItem.setMenu(menu);

    for (int i = 0; i < 5; i++) {
        MenuItem item = new MenuItem(menu, SWT.PUSH);
        item.setText("Item " + i);
        item.addArmListener(e -> statusLine.setText(((MenuItem) e.getSource()).getText()));
    }

    shell.pack();
    shell.open();

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

From source file:Snippet153.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 200, 200);//w  ww  .  j  av  a  2 s .c o m
    final ToolBar bar = new ToolBar(shell, SWT.BORDER);
    bar.setBounds(10, 10, 150, 50);
    final Label statusLine = new Label(shell, SWT.BORDER);
    statusLine.setBounds(10, 90, 150, 30);
    new ToolItem(bar, SWT.NONE).setText("item 1");
    new ToolItem(bar, SWT.NONE).setText("item 2");
    new ToolItem(bar, SWT.NONE).setText("item 3");
    bar.addMouseMoveListener(new MouseMoveListener() {
        public void mouseMove(MouseEvent e) {
            ToolItem item = bar.getItem(new Point(e.x, e.y));
            String name = "";
            if (item != null) {
                name = item.getText();
            }
            if (!statusText.equals(name)) {
                statusLine.setText(name);
                statusText = name;
            }
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}