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

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

    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);
    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);//from  w w  w.  j  a  va 2  s.c o m

    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();
            statusLabel.setText(root.getAbsolutePath());
            locationText.setText(root.getAbsolutePath());
        }
    });

    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;
    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.Snippet166.java

public static void main(String[] args) {
    Display display = new Display();
    Image image1 = display.getSystemImage(SWT.ICON_WORKING);
    Image image2 = display.getSystemImage(SWT.ICON_QUESTION);
    Image image3 = display.getSystemImage(SWT.ICON_ERROR);

    Shell shell = new Shell(display);
    shell.setText("Snippet 166");
    shell.setLayout(new FillLayout());

    final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.BORDER);

    final Composite parent = new Composite(scrollComposite, SWT.NONE);
    for (int i = 0; i <= 50; i++) {
        Label label = new Label(parent, SWT.NONE);
        if (i % 3 == 0)
            label.setImage(image1);/*from  ww  w .ja v a  2 s  .c o m*/
        if (i % 3 == 1)
            label.setImage(image2);
        if (i % 3 == 2)
            label.setImage(image3);
    }
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;
    parent.setLayout(layout);

    scrollComposite.setContent(parent);
    scrollComposite.setExpandVertical(true);
    scrollComposite.setExpandHorizontal(true);
    scrollComposite.addControlListener(ControlListener.controlResizedAdapter(e -> {
        Rectangle r = scrollComposite.getClientArea();
        scrollComposite.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
    }));

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

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 144");
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    final Table table = new Table(shell, SWT.VIRTUAL | SWT.BORDER);
    table.addListener(SWT.SetData, event -> {
        TableItem item = (TableItem) event.item;
        int index = table.indexOf(item);
        item.setText("Item " + index);
        System.out.println(item.getText());
    });//  w  ww.  j av a2s.c  o  m
    table.setLayoutData(new RowData(200, 200));
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Add Items");
    final Label label = new Label(shell, SWT.NONE);
    button.addListener(SWT.Selection, event -> {
        long t1 = System.currentTimeMillis();
        table.setItemCount(COUNT);
        long t2 = System.currentTimeMillis();
        label.setText("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms)");
        shell.layout();
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:DragDropOperation.java

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

    final Label label = new Label(shell, SWT.BORDER);
    label.setText("Drag Source");
    DragSource source = new DragSource(label, DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK);
    source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {
        public void dragSetData(DragSourceEvent event) {
            event.data = "Text Transferred";
        }//from w  ww. ja v a  2s .  co m

        public void dragFinished(DragSourceEvent event) {
            if (event.doit) {
                String operation = null;
                switch (event.detail) {
                case DND.DROP_MOVE:
                    operation = "moved";
                    break;
                case DND.DROP_COPY:
                    operation = "copied";
                    break;
                case DND.DROP_LINK:
                    operation = "linked";
                    break;
                case DND.DROP_NONE:
                    operation = "disallowed";
                    break;
                default:
                    operation = "unknown";
                    break;
                }
                label.setText("Drag Source (data " + operation + ")");
            } else {
                label.setText("Drag Source (drag cancelled)");
            }
        }
    });

    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI);
    text.setText("Drop Target");
    DropTarget target = new DropTarget(text, DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK);
    target.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
        public void dragEnter(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT)
                event.detail = DND.DROP_COPY;
        }

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

        public void drop(DropTargetEvent event) {
            String operation = null;
            switch (event.detail) {
            case DND.DROP_MOVE:
                operation = "moved";
                break;
            case DND.DROP_COPY:
                operation = "copied";
                break;
            case DND.DROP_LINK:
                operation = "linked";
                break;
            case DND.DROP_NONE:
                operation = "disallowed";
                break;
            default:
                operation = "unknown";
                break;
            }
            text.append("\n" + operation + (String) event.data);
        }
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    errorIcon = display.getSystemImage(SWT.ICON_ERROR);
    Shell shell = new Shell(display);
    shell.setText("Snippet 363");
    shell.setLayout(new GridLayout(2, false));
    shell.setText("LiveRegion Test");

    icon = new Label(shell, SWT.NONE);
    icon.setLayoutData(new GridData(32, 32));

    liveLabel = new Text(shell, SWT.READ_ONLY);
    GC gc = new GC(liveLabel);
    Point pt = gc.textExtent(errorMessage);
    GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
    data.minimumWidth = (int) (pt.x + gc.getFontMetrics().getAverageCharacterWidth() * 2);
    gc.dispose();//ww  w. ja  v a2  s .c  o m
    liveLabel.setLayoutData(data);
    liveLabel.setText("");
    liveLabel.getAccessible().addAccessibleAttributeListener(new AccessibleAttributeAdapter() {
        @Override
        public void getAttributes(AccessibleAttributeEvent e) {
            e.attributes = new String[] { "container-live", "polite", "live", "polite", "container-live-role",
                    "status", };
        }
    });

    final Label textFieldLabel = new Label(shell, SWT.NONE);
    textFieldLabel.setText("Type a number:");
    textFieldLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

    final Text textField = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

    final Button okButton = new Button(shell, SWT.PUSH);
    okButton.setText("OK");
    okButton.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false, 2, 1));
    okButton.setEnabled(false);

    textField.addModifyListener(e -> {
        boolean isNumber = false;
        String textValue = textField.getText();
        try {
            Integer.parseInt(textValue);
            isNumber = true;
            setMessageText(false, "Thank-you");
        } catch (NumberFormatException ex) {
            if (textValue.isEmpty()) {
                setMessageText(false, "");
            } else {
                setMessageText(true, "Error: Number expected.");
            }
        }
        okButton.setEnabled(isNumber);
    });

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

From source file:Snippet251.java

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

    Button open = new Button(shell, SWT.PUSH);
    open.setText("Open Dialog");
    open.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
            dialog.setLayout(new GridLayout(3, false));

            final DateTime calendar = new DateTime(dialog, SWT.CALENDAR | SWT.BORDER);
            final DateTime date = new DateTime(dialog, SWT.DATE | SWT.SHORT);
            final DateTime time = new DateTime(dialog, SWT.TIME | SWT.SHORT);

            new Label(dialog, SWT.NONE);
            new Label(dialog, SWT.NONE);
            Button ok = new Button(dialog, SWT.PUSH);
            ok.setText("OK");
            ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
            ok.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println("Calendar date selected (MM/DD/YYYY) = " + (calendar.getMonth() + 1)
                            + "/" + calendar.getDay() + "/" + calendar.getYear());
                    System.out.println(
                            "Date selected (MM/YYYY) = " + (date.getMonth() + 1) + "/" + date.getYear());
                    System.out.println("Time selected (HH:MM) = " + time.getHours() + ":" + time.getMinutes());
                    dialog.close();//from w  w w .j a  v  a2s .  c o  m
                }
            });
            dialog.setDefaultButton(ok);
            dialog.pack();
            dialog.open();
        }
    });
    shell.pack();
    shell.open();

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

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

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

    Button open = new Button(shell, SWT.PUSH);
    open.setText("Open Dialog");
    open.addSelectionListener(widgetSelectedAdapter(e -> {
        final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
        dialog.setLayout(new GridLayout(3, false));

        final DateTime calendar = new DateTime(dialog, SWT.CALENDAR | SWT.BORDER);
        final DateTime date = new DateTime(dialog, SWT.DATE | SWT.SHORT);
        final DateTime time = new DateTime(dialog, SWT.TIME | SWT.SHORT);

        new Label(dialog, SWT.NONE);
        new Label(dialog, SWT.NONE);
        Button ok = new Button(dialog, SWT.PUSH);
        ok.setText("OK");
        ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        ok.addSelectionListener(widgetSelectedAdapter(event -> {
            System.out.println("Calendar date selected (MM/DD/YYYY) = " + (calendar.getMonth() + 1) + "/"
                    + calendar.getDay() + "/" + calendar.getYear());
            System.out.println("Date selected (MM/YYYY) = " + (date.getMonth() + 1) + "/" + date.getYear());
            System.out.println("Time selected (HH:MM) = " + time.getHours() + ":"
                    + (time.getMinutes() < 10 ? "0" : "") + time.getMinutes());
            dialog.close();//from  w  ww  . j av  a  2s. c om
        }));
        dialog.setDefaultButton(ok);
        dialog.pack();
        dialog.open();
    }));
    shell.pack();
    shell.open();

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 249");
    Rectangle clientArea = shell.getClientArea();
    shell.setBounds(clientArea.x + 10, clientArea.y + 10, 300, 200);
    // create the composite that the pages will share
    final Composite contentPanel = new Composite(shell, SWT.BORDER);
    contentPanel.setBounds(clientArea.x + 100, clientArea.y + 10, 190, 90);
    final StackLayout layout = new StackLayout();
    contentPanel.setLayout(layout);//from   w  w w  . ja  v a2s  . com

    // create the first page's content
    final Composite page0 = new Composite(contentPanel, SWT.NONE);
    page0.setLayout(new RowLayout());
    Label label = new Label(page0, SWT.NONE);
    label.setText("Label on page 1");
    label.pack();

    // create the second page's content
    final Composite page1 = new Composite(contentPanel, SWT.NONE);
    page1.setLayout(new RowLayout());
    Button button = new Button(page1, SWT.NONE);
    button.setText("Button on page 2");
    button.pack();

    // create the button that will switch between the pages
    Button pageButton = new Button(shell, SWT.PUSH);
    pageButton.setText("Push");
    pageButton.setBounds(clientArea.x + 10, clientArea.y + 10, 80, 25);
    pageButton.addListener(SWT.Selection, event -> {
        pageNum = ++pageNum % 2;
        layout.topControl = pageNum == 0 ? page0 : page1;
        contentPanel.layout();
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 292");
    final Group group = new Group(shell, SWT.NONE);
    group.setText("Group");
    group.setLayout(new GridLayout());
    final Tree tree = new Tree(group, SWT.BORDER);
    for (int i = 0; i < 5; i++) {
        TreeItem treeItem = new TreeItem(tree, SWT.NONE);
        treeItem.setText("TreeItem " + i);
        for (int j = 0; j < 3; j++) {
            TreeItem subItem = new TreeItem(treeItem, SWT.NONE);
            subItem.setText("SubItem " + i + "-" + j);
        }/*from   w  ww .j  av  a2 s  .com*/
        if (i % 3 == 0)
            treeItem.setExpanded(true);
    }
    new Button(group, SWT.PUSH).setText("Button");
    final Label label = new Label(shell, SWT.NONE);
    label.addListener(SWT.Dispose, e -> {
        Image image = label.getImage();
        if (image != null)
            image.dispose();
    });
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Snapshot");
    button.addListener(SWT.Selection, e -> {
        Image image = label.getImage();
        if (image != null)
            image.dispose();
        image = new Image(display, group.getBounds());
        GC gc = new GC(image);
        boolean success = group.print(gc);
        gc.dispose();
        label.setImage(image);
        if (!success) {
            MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.PRIMARY_MODAL);
            messageBox.setMessage("Sorry, taking a snapshot is not supported on your platform");
            messageBox.open();
        }
    });
    GridLayout layout = new GridLayout(2, true);
    shell.setLayout(layout);
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

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

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

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

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

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

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

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

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