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

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

Introduction

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

Prototype

public Label(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

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

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

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

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

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

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

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

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

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

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

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

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

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

    Group group = new Group(shell, SWT.NONE);
    group.setText("Tables With Accessible Cell Children");
    group.setLayout(new GridLayout());

    new Label(group, SWT.NONE).setText("CTable with column headers");

    table1 = new CTable(group, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    table1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    table1.setHeaderVisible(true);//from  w  ww. j  av a 2  s  .c  o  m
    table1.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        CTableColumn column = new CTableColumn(table1, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int row = 0; row < 4; row++) {
        CTableItem item = new CTableItem(table1, SWT.NONE);
        item.setText(new String[] { "C0R" + row, "C1R" + row, "C2R" + row });
    }

    Composite btnGroup = new Composite(group, SWT.NONE);
    btnGroup.setLayout(new FillLayout(SWT.VERTICAL));
    Button btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Add rows");
    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        int currSize = table1.getItemCount();
        int colCount = table1.getColumnCount();
        CTableItem item = new CTableItem(table1, SWT.NONE);
        String[] cells = new String[colCount];

        for (int i = 0; i < colCount; i++) {
            cells[i] = "C" + i + "R" + currSize;
        }
        item.setText(cells);
    }));
    btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Remove rows");
    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        int currSize = table1.getItemCount();
        if (currSize > 0) {
            table1.remove(currSize - 1);
        }
    }));
    btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Remove selected rows");
    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        CTableItem[] selectedItems = table1.getSelection();
        for (CTableItem selectedItem : selectedItems) {
            selectedItem.dispose();
        }
    }));
    btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Add column");
    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        int currSize = table1.getColumnCount();
        CTableColumn item = new CTableColumn(table1, SWT.NONE);
        item.setText("Col " + currSize);
        item.setWidth(50);
    }));
    btn = new Button(btnGroup, SWT.PUSH);
    btn.setText("Remove last column");

    btn.addSelectionListener(widgetSelectedAdapter(e -> {
        int colCount = table1.getColumnCount();
        if (colCount > 0) {
            CTableColumn column = table1.getColumn(colCount - 1);
            column.dispose();
        }
    }));

    new Label(group, SWT.NONE).setText("CTable used as a list");

    CTable table2 = new CTable(group, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    table2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    table2.setLinesVisible(true);
    for (String element : itemText) {
        CTableItem item = new CTableItem(table2, SWT.NONE);
        item.setText(element);
    }

    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);
        }//  w w w.ja  v a  2 s.c  om
        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:Snippet125.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER);
    for (int i = 0; i < 20; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText("item " + i);
    }//w  w  w.jav a2  s  .  c  o m
    // Disable native tooltip
    table.setToolTipText("");

    // Implement a "fake" tooltip
    final Listener labelListener = new Listener() {
        public void handleEvent(Event event) {
            Label label = (Label) event.widget;
            Shell shell = label.getShell();
            switch (event.type) {
            case SWT.MouseDown:
                Event e = new Event();
                e.item = (TableItem) label.getData("_TABLEITEM");
                // Assuming table is single select, set the selection as if
                // the mouse down event went through to the table
                table.setSelection(new TableItem[] { (TableItem) e.item });
                table.notifyListeners(SWT.Selection, e);
                // fall through
            case SWT.MouseExit:
                shell.dispose();
                break;
            }
        }
    };

    Listener tableListener = new Listener() {
        Shell tip = null;

        Label label = null;

        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.Dispose:
            case SWT.KeyDown:
            case SWT.MouseMove: {
                if (tip == null)
                    break;
                tip.dispose();
                tip = null;
                label = null;
                break;
            }
            case SWT.MouseHover: {
                TableItem item = table.getItem(new Point(event.x, event.y));
                if (item != null) {
                    if (tip != null && !tip.isDisposed())
                        tip.dispose();
                    tip = new Shell(shell, SWT.ON_TOP | SWT.TOOL);
                    tip.setLayout(new FillLayout());
                    label = new Label(tip, SWT.NONE);
                    label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
                    label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
                    label.setData("_TABLEITEM", item);
                    label.setText("tooltip " + item.getText());
                    label.addListener(SWT.MouseExit, labelListener);
                    label.addListener(SWT.MouseDown, labelListener);
                    Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
                    Rectangle rect = item.getBounds(0);
                    Point pt = table.toDisplay(rect.x, rect.y);
                    tip.setBounds(pt.x, pt.y, size.x, size.y);
                    tip.setVisible(true);
                }
            }
            }
        }
    };
    table.addListener(SWT.Dispose, tableListener);
    table.addListener(SWT.KeyDown, tableListener);
    table.addListener(SWT.MouseMove, tableListener);
    table.addListener(SWT.MouseHover, tableListener);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final ImageFileNameProvider filenameProvider = zoom -> {
        switch (zoom) {
        case 100:
            return IMAGE_PATH_100;
        case 150:
            return IMAGE_PATH_150;
        case 200:
            return IMAGE_PATH_200;
        default://ww  w.ja  v  a 2 s  . c o  m
            return null;
        }
    };
    final ImageDataProvider imageDataProvider = zoom -> {
        switch (zoom) {
        case 100:
            return new ImageData(IMAGE_PATH_100);
        case 150:
            return new ImageData(IMAGE_PATH_150);
        case 200:
            return new ImageData(IMAGE_PATH_200);
        default:
            return null;
        }
    };

    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet367");
    shell.setLayout(new GridLayout(3, false));

    Menu menuBar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(menuBar);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit");
    exitItem.addListener(SWT.Selection, e -> shell.close());

    new Label(shell, SWT.NONE).setText(IMAGE_200 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_200));
    new Button(shell, SWT.PUSH).setImage(new Image(display, IMAGE_PATH_200));

    new Label(shell, SWT.NONE).setText(IMAGE_150 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150));
    new Button(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150));

    new Label(shell, SWT.NONE).setText(IMAGE_100 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100));
    new Button(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("ImageFileNameProvider:");
    new Label(shell, SWT.NONE).setImage(new Image(display, filenameProvider));
    new Button(shell, SWT.NONE).setImage(new Image(display, filenameProvider));

    new Label(shell, SWT.NONE).setText("ImageDataProvider:");
    new Label(shell, SWT.NONE).setImage(new Image(display, imageDataProvider));
    new Button(shell, SWT.NONE).setImage(new Image(display, imageDataProvider));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("1. Canvas\n(PaintListener)");
    final Point size = new Point(550, 40);
    final Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.addPaintListener(e -> {
        Point size1 = canvas.getSize();
        paintImage(e.gc, size1);
    });
    GridData gridData = new GridData(size.x, size.y);
    gridData.horizontalSpan = 2;
    canvas.setLayoutData(gridData);

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("2. Painted image\n (default resolution)");
    Image image = new Image(display, size.x, size.y);
    GC gc = new GC(image);
    try {
        paintImage(gc, size);
    } finally {
        gc.dispose();
    }
    Label imageLabel = new Label(shell, SWT.NONE);
    imageLabel.setImage(image);
    imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("3. Painted image\n(multi-res, unzoomed paint)");
    imageLabel = new Label(shell, SWT.NONE);
    imageLabel.setImage(new Image(display, (ImageDataProvider) zoom -> {
        Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100);
        GC gc1 = new GC(temp);
        try {
            paintImage(gc1, size);
            return temp.getImageData();
        } finally {
            gc1.dispose();
            temp.dispose();
        }
    }));
    imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("4. Painted image\n(multi-res, zoomed paint)");
    imageLabel = new Label(shell, SWT.NONE);
    imageLabel.setImage(new Image(display, (ImageDataProvider) zoom -> {
        Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100);
        GC gc1 = new GC(temp);
        try {
            paintImage2(gc1, new Point(size.x * zoom / 100, size.y * zoom / 100), zoom / 100);
            return temp.getImageData();
        } finally {
            gc1.dispose();
            temp.dispose();
        }
    }));
    imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
    Label box = new Label(shell, SWT.NONE);
    box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
    box.setLayoutData(new GridData(50, 50));

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

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

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

    Composite c1 = new Composite(shell, SWT.BORDER);
    c1.setLayout(new RowLayout());
    Button b1 = new Button(c1, SWT.PUSH);
    b1.setText("B&1");
    Button r1 = new Button(c1, SWT.RADIO);
    r1.setText("R1");
    Button r2 = new Button(c1, SWT.RADIO);
    r2.setText("R&2");
    Button r3 = new Button(c1, SWT.RADIO);
    r3.setText("R3");
    Button b2 = new Button(c1, SWT.PUSH);
    b2.setText("B2");
    List l1 = new List(c1, SWT.SINGLE | SWT.BORDER);
    l1.setItems("L1");
    Button b3 = new Button(c1, SWT.PUSH);
    b3.setText("B&3");
    Button b4 = new Button(c1, SWT.PUSH);
    b4.setText("B&4");

    Composite c2 = new Composite(shell, SWT.BORDER);
    c2.setLayout(new RowLayout());
    Button b5 = new Button(c2, SWT.PUSH);
    b5.setText("B&5");
    Button b6 = new Button(c2, SWT.PUSH);
    b6.setText("B&6");

    List l2 = new List(shell, SWT.SINGLE | SWT.BORDER);
    l2.setItems("L2");

    ToolBar tb1 = new ToolBar(shell, SWT.FLAT | SWT.BORDER);
    ToolItem i1 = new ToolItem(tb1, SWT.RADIO);
    i1.setText("I1");
    ToolItem i2 = new ToolItem(tb1, SWT.RADIO);
    i2.setText("I2");
    Combo combo1 = new Combo(tb1, SWT.READ_ONLY | SWT.BORDER);
    combo1.setItems("C1");
    combo1.setText("C1");
    combo1.pack();//from w  w  w  .  java 2s .com
    ToolItem i3 = new ToolItem(tb1, SWT.SEPARATOR);
    i3.setWidth(combo1.getSize().x);
    i3.setControl(combo1);
    ToolItem i4 = new ToolItem(tb1, SWT.PUSH);
    i4.setText("I&4");
    ToolItem i5 = new ToolItem(tb1, SWT.CHECK);
    i5.setText("I5");

    Button b7 = new Button(shell, SWT.PUSH);
    b7.setText("B&7");

    Composite c4 = new Composite(shell, SWT.BORDER);
    Composite c5 = new Composite(c4, SWT.BORDER);
    c5.setLayout(new FillLayout());
    new Label(c5, SWT.NONE).setText("No");
    c5.pack();

    Control[] tabList1 = new Control[] { b2, b1, b3 };
    c1.setTabList(tabList1);
    Control[] tabList2 = new Control[] { c1, b7, tb1, c4, c2, l2 };
    shell.setTabList(tabList2);

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

    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 .ja  v a2  s .  com
    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:org.eclipse.swt.snippets.Snippet125.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 125");
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER);
    for (int i = 0; i < 20; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText("item " + i);
    }//from   ww w.  ja v  a  2s . co m
    // Disable native tooltip
    table.setToolTipText("");

    // Implement a "fake" tooltip
    final Listener labelListener = event -> {
        Label label = (Label) event.widget;
        Shell shell1 = label.getShell();
        switch (event.type) {
        case SWT.MouseDown:
            Event e = new Event();
            e.item = (TableItem) label.getData("_TABLEITEM");
            // Assuming table is single select, set the selection as if
            // the mouse down event went through to the table
            table.setSelection(new TableItem[] { (TableItem) e.item });
            table.notifyListeners(SWT.Selection, e);
            shell1.dispose();
            table.setFocus();
            break;
        case SWT.MouseExit:
            shell1.dispose();
            break;
        }
    };

    Listener tableListener = new Listener() {
        Shell tip = null;
        Label label = null;

        @Override
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.Dispose:
            case SWT.KeyDown:
            case SWT.MouseMove: {
                if (tip == null)
                    break;
                tip.dispose();
                tip = null;
                label = null;
                break;
            }
            case SWT.MouseHover: {
                TableItem item = table.getItem(new Point(event.x, event.y));
                if (item != null) {
                    if (tip != null && !tip.isDisposed())
                        tip.dispose();
                    tip = new Shell(shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
                    tip.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
                    FillLayout layout = new FillLayout();
                    layout.marginWidth = 2;
                    tip.setLayout(layout);
                    label = new Label(tip, SWT.NONE);
                    label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
                    label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
                    label.setData("_TABLEITEM", item);
                    label.setText(item.getText());
                    label.addListener(SWT.MouseExit, labelListener);
                    label.addListener(SWT.MouseDown, labelListener);
                    Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
                    Rectangle rect = item.getBounds(0);
                    Point pt = table.toDisplay(rect.x, rect.y);
                    tip.setBounds(pt.x, pt.y, size.x, size.y);
                    tip.setVisible(true);
                }
            }
            }
        }
    };
    table.addListener(SWT.Dispose, tableListener);
    table.addListener(SWT.KeyDown, tableListener);
    table.addListener(SWT.MouseMove, tableListener);
    table.addListener(SWT.MouseHover, tableListener);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout(4, true));
    shell.setText("All Controls Test");

    new Label(shell, SWT.NONE).setText("Label for Label");
    label = new Label(shell, SWT.NONE);
    label.setText("Label");

    new Label(shell, SWT.NONE).setText("Label for CLabel");
    cLabel = new CLabel(shell, SWT.NONE);
    cLabel.setText("CLabel");

    new Label(shell, SWT.NONE).setText("Label for Push Button");
    buttonPush = new Button(shell, SWT.PUSH);
    buttonPush.setText("Push Button");

    new Label(shell, SWT.NONE).setText("Label for Radio Button");
    buttonRadio = new Button(shell, SWT.RADIO);
    buttonRadio.setText("Radio Button");

    new Label(shell, SWT.NONE).setText("Label for Check Button");
    buttonCheck = new Button(shell, SWT.CHECK);
    buttonCheck.setText("Check Button");

    new Label(shell, SWT.NONE).setText("Label for Toggle Button");
    buttonToggle = new Button(shell, SWT.TOGGLE);
    buttonToggle.setText("Toggle Button");

    new Label(shell, SWT.NONE).setText("Label for Editable Combo");
    combo = new Combo(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        combo.add("item" + i);
    }//  www  . j  ava2  s.c  o m
    combo.select(0);

    new Label(shell, SWT.NONE).setText("Label for Read-Only Combo");
    combo = new Combo(shell, SWT.READ_ONLY | SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        combo.add("item" + i);
    }
    combo.select(0);

    new Label(shell, SWT.NONE).setText("Label for CCombo");
    cCombo = new CCombo(shell, SWT.BORDER);
    for (int i = 0; i < 5; i++) {
        cCombo.add("item" + i);
    }
    cCombo.select(0);

    new Label(shell, SWT.NONE).setText("Label for List");
    list = new List(shell, SWT.SINGLE | SWT.BORDER);
    list.setItems("Item0", "Item1", "Item2");

    new Label(shell, SWT.NONE).setText("Label for Spinner");
    spinner = new Spinner(shell, SWT.BORDER);

    new Label(shell, SWT.NONE).setText("Label for Single-line Text");
    textSingle = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textSingle.setText("Contents of Single-line Text");

    new Label(shell, SWT.NONE).setText("Label for Multi-line Text");
    textMulti = new Text(shell, SWT.MULTI | SWT.BORDER);
    textMulti.setText("\nContents of Multi-line Text\n");

    new Label(shell, SWT.NONE).setText("Label for StyledText");
    styledText = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    styledText.setText("\nContents of Multi-line StyledText\n");

    new Label(shell, SWT.NONE).setText("Label for Table");
    table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int row = 0; row < 3; row++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "C0R" + row, "C1R" + row, "C2R" + row });
    }

    new Label(shell, SWT.NONE).setText("Label for Tree");
    tree = new Tree(shell, SWT.BORDER | SWT.MULTI);
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("Item" + i);
        for (int j = 0; j < 4; j++) {
            new TreeItem(item, SWT.NONE).setText("Item" + i + j);
        }
    }

    new Label(shell, SWT.NONE).setText("Label for Tree with columns");
    treeTable = new Tree(shell, SWT.BORDER | SWT.MULTI);
    treeTable.setHeaderVisible(true);
    treeTable.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        TreeColumn column = new TreeColumn(treeTable, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(treeTable, SWT.NONE);
        item.setText(new String[] { "I" + i + "C0", "I" + i + "C1", "I" + i + "C2" });
        for (int j = 0; j < 4; j++) {
            new TreeItem(item, SWT.NONE)
                    .setText(new String[] { "I" + i + j + "C0", "I" + i + j + "C1", "I" + i + j + "C2" });
        }
    }

    new Label(shell, SWT.NONE).setText("Label for ToolBar");
    toolBar = new ToolBar(shell, SWT.FLAT);
    for (int i = 0; i < 3; i++) {
        ToolItem item = new ToolItem(toolBar, SWT.PUSH);
        item.setText("Item" + i);
        item.setToolTipText("ToolItem ToolTip" + i);
    }

    new Label(shell, SWT.NONE).setText("Label for CoolBar");
    coolBar = new CoolBar(shell, SWT.FLAT);
    for (int i = 0; i < 2; i++) {
        CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
        ToolBar coolItemToolBar = new ToolBar(coolBar, SWT.FLAT);
        int toolItemWidth = 0;
        for (int j = 0; j < 2; j++) {
            ToolItem item = new ToolItem(coolItemToolBar, SWT.PUSH);
            item.setText("Item" + i + j);
            item.setToolTipText("ToolItem ToolTip" + i + j);
            if (item.getWidth() > toolItemWidth)
                toolItemWidth = item.getWidth();
        }
        coolItem.setControl(coolItemToolBar);
        Point size = coolItemToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        Point coolSize = coolItem.computeSize(size.x, size.y);
        coolItem.setMinimumSize(toolItemWidth, coolSize.y);
        coolItem.setPreferredSize(coolSize);
        coolItem.setSize(coolSize);
    }

    new Label(shell, SWT.NONE).setText("Label for Canvas");
    canvas = new Canvas(shell, SWT.BORDER);
    canvas.setLayoutData(new GridData(64, 64));
    canvas.addPaintListener(e -> e.gc.drawString("Canvas", 15, 25));
    canvas.setCaret(new Caret(canvas, SWT.NONE));
    /* Hook key listener so canvas will take focus during traversal in. */
    canvas.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    /* Hook traverse listener to make canvas give up focus during traversal out. */
    canvas.addTraverseListener(e -> e.doit = true);

    new Label(shell, SWT.NONE).setText("Label for Group");
    group = new Group(shell, SWT.NONE);
    group.setText("Group");
    group.setLayout(new FillLayout());
    new Text(group, SWT.SINGLE | SWT.BORDER).setText("Text in Group");

    new Label(shell, SWT.NONE).setText("Label for TabFolder");
    tabFolder = new TabFolder(shell, SWT.NONE);
    for (int i = 0; i < 3; i++) {
        TabItem item = new TabItem(tabFolder, SWT.NONE);
        item.setText("TabItem &" + i);
        item.setToolTipText("TabItem ToolTip" + i);
        Text itemText = new Text(tabFolder, SWT.SINGLE | SWT.BORDER);
        itemText.setText("Text for TabItem " + i);
        item.setControl(itemText);
    }

    new Label(shell, SWT.NONE).setText("Label for CTabFolder");
    cTabFolder = new CTabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
        CTabItem item = new CTabItem(cTabFolder, SWT.NONE);
        item.setText("CTabItem &" + i);
        item.setToolTipText("CTabItem ToolTip" + i);
        Text itemText = new Text(cTabFolder, SWT.SINGLE | SWT.BORDER);
        itemText.setText("Text for CTabItem " + i);
        item.setControl(itemText);
    }
    cTabFolder.setSelection(cTabFolder.getItem(0));

    new Label(shell, SWT.NONE).setText("Label for Scale");
    scale = new Scale(shell, SWT.NONE);

    new Label(shell, SWT.NONE).setText("Label for Slider");
    slider = new Slider(shell, SWT.NONE);

    new Label(shell, SWT.NONE).setText("Label for ProgressBar");
    progressBar = new ProgressBar(shell, SWT.NONE);
    progressBar.setSelection(50);

    new Label(shell, SWT.NONE).setText("Label for Sash");
    sash = new Sash(shell, SWT.NONE);

    shell.pack();
    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 -> {//  ww 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();
}