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.Snippet84.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 84");
    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(TextTransfer.getInstance());
    source.addDragListener(new DragSourceAdapter() {
        @Override/*from   w w  w . j a  va 2s .  c om*/
        public void dragSetData(DragSourceEvent event) {
            event.data = "Text Transferred";
        }

        @Override
        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(TextTransfer.getInstance());
    target.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) {
            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.Snippet350.java

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

    Label nameLabel = new Label(shell, SWT.NONE);
    nameLabel.setText("Name:");
    Text nameText = new Text(shell, SWT.BORDER);
    nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Accessible accNameLabel = nameLabel.getAccessible();
    Accessible accNameText = nameText.getAccessible();
    accNameLabel.addRelation(ACC.RELATION_LABEL_FOR, accNameText);
    accNameText.addRelation(ACC.RELATION_LABELLED_BY, accNameLabel);

    Group addressGroup = new Group(shell, SWT.NONE);
    addressGroup.setText("Address");
    addressGroup.setLayout(new GridLayout(2, false));
    addressGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

    Label streetLabel = new Label(addressGroup, SWT.NONE);
    streetLabel.setText("Street:");
    Text streetText = new Text(addressGroup, SWT.BORDER);
    streetText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Accessible accStreetLabel = streetLabel.getAccessible();
    Accessible accStreetText = streetText.getAccessible();
    accStreetLabel.addRelation(ACC.RELATION_LABEL_FOR, accStreetText);
    accStreetText.addRelation(ACC.RELATION_LABELLED_BY, accStreetLabel);

    Label cityLabel = new Label(addressGroup, SWT.NONE);
    cityLabel.setText("City:");
    Text cityText = new Text(addressGroup, SWT.BORDER);
    cityText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Accessible accCityLabel = cityLabel.getAccessible();
    Accessible accCityText = cityText.getAccessible();
    accCityLabel.addRelation(ACC.RELATION_LABEL_FOR, accCityText);
    accCityText.addRelation(ACC.RELATION_LABELLED_BY, accCityLabel);

    Accessible accAddressGroup = addressGroup.getAccessible();
    accStreetText.addRelation(ACC.RELATION_MEMBER_OF, accAddressGroup);
    accStreetText.addRelation(ACC.RELATION_LABELLED_BY, accAddressGroup);
    accCityText.addRelation(ACC.RELATION_MEMBER_OF, accAddressGroup);
    accCityText.addRelation(ACC.RELATION_LABELLED_BY, accAddressGroup);

    shell.pack();/* www  . j av  a  2s  .c o  m*/
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 185");
    shell.setLayout(new FillLayout());
    Label label1 = new Label(shell, SWT.BORDER);
    label1.setText("Drag Source");
    final Table table = new Table(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        if (i % 2 == 0)
            item.setText("Drop a file");
        if (i % 2 == 1)
            item.setText("Drop text");
    }/*  w  w  w.j  a v  a  2  s. com*/
    DragSource dragSource = new DragSource(label1, DND.DROP_COPY);
    dragSource.setTransfer(TextTransfer.getInstance(), FileTransfer.getInstance());
    dragSource.addDragListener(new DragSourceAdapter() {
        @Override
        public void dragSetData(DragSourceEvent event) {
            if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
                File file = new File("temp");
                event.data = new String[] { file.getAbsolutePath() };
            }
            if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = "once upon a time";
            }
        }
    });
    DropTarget dropTarget = new DropTarget(table, DND.DROP_COPY | DND.DROP_DEFAULT);
    dropTarget.setTransfer(TextTransfer.getInstance(), FileTransfer.getInstance());
    dropTarget.addDropListener(new DropTargetAdapter() {
        FileTransfer fileTransfer = FileTransfer.getInstance();
        TextTransfer textTransfer = TextTransfer.getInstance();

        @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 dragOver(DropTargetEvent event) {
            event.detail = DND.DROP_NONE;
            TableItem item = (TableItem) event.item;
            if (item == null)
                return;
            int itemIndex = table.indexOf(item);
            if (itemIndex % 2 == 0) {
                int index = 0;
                while (index < event.dataTypes.length) {
                    if (fileTransfer.isSupportedType(event.dataTypes[index]))
                        break;
                    index++;
                }
                if (index < event.dataTypes.length) {
                    event.currentDataType = event.dataTypes[index];
                    event.detail = DND.DROP_COPY;
                    return;
                }
            } else {
                int index = 0;
                while (index < event.dataTypes.length) {
                    if (textTransfer.isSupportedType(event.dataTypes[index]))
                        break;
                    index++;
                }
                if (index < event.dataTypes.length) {
                    event.currentDataType = event.dataTypes[index];
                    event.detail = DND.DROP_COPY;
                    return;
                }
            }
        }

        @Override
        public void drop(DropTargetEvent event) {
            TableItem item = (TableItem) event.item;
            if (item == null) {
                event.detail = DND.DROP_NONE;
                return;
            }
            if (fileTransfer.isSupportedType(event.currentDataType)) {
                String[] files = (String[]) event.data;
                if (files != null && files.length > 0) {
                    item.setText(files[0]);
                }
            }
            if (textTransfer.isSupportedType(event.currentDataType)) {
                String text = (String) event.data;
                if (text != null) {
                    item.setText(text);
                }
            }
        }

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

From source file:DialogOKCancelFormLayout.java

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

    Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
    Label label = new Label(dialog, SWT.NONE);
    label.setText("Exit the application?");
    Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setText("&OK");
    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setText("&Cancel");

    FormLayout form = new FormLayout();
    form.marginWidth = form.marginHeight = 8;
    dialog.setLayout(form);/*w w  w. j av a 2 s.  co  m*/
    FormData okData = new FormData();
    okData.top = new FormAttachment(label, 8);
    okButton.setLayoutData(okData);
    FormData cancelData = new FormData();
    cancelData.left = new FormAttachment(okButton, 8);
    cancelData.top = new FormAttachment(okButton, 0, SWT.TOP);
    cancelButton.setLayoutData(cancelData);

    dialog.setDefaultButton(okButton);
    dialog.pack();
    dialog.open();

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

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

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

    SashForm form = new SashForm(shell, SWT.HORIZONTAL);
    form.setLayout(new FillLayout());

    Composite child1 = new Composite(form, SWT.NONE);
    child1.setLayout(new FillLayout());
    new Label(child1, SWT.NONE).setText("Label in pane 1");

    Composite child2 = new Composite(form, SWT.NONE);
    child2.setLayout(new FillLayout());
    new Button(child2, SWT.PUSH).setText("Button in pane2");

    Composite child3 = new Composite(form, SWT.NONE);
    child3.setLayout(new FillLayout());
    new Label(child3, SWT.PUSH).setText("Label in pane3");

    form.setWeights(new int[] { 30, 40, 30 });
    shell.open();/*w  w  w .  j  a  va2 s . c o m*/
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:DialogClass.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Dialog Example");
    shell.setSize(300, 200);/*from  w ww  .  ja v a 2  s.  co m*/
    shell.open();

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Delete File");
    button.setBounds(20, 40, 80, 25);

    final Text text = new Text(shell, SWT.SHADOW_IN);
    text.setBounds(140, 40, 100, 25);

    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
    dialog.setText("Delete File");
    dialog.setSize(250, 150);

    final Button buttonOK = new Button(dialog, SWT.PUSH);
    buttonOK.setText("OK");
    buttonOK.setBounds(20, 55, 80, 25);

    Button buttonCancel = new Button(dialog, SWT.PUSH);
    buttonCancel.setText("Cancel");
    buttonCancel.setBounds(120, 55, 80, 25);

    final Label label = new Label(dialog, SWT.NONE);
    label.setText("Delete the file?");
    label.setBounds(20, 15, 100, 20);

    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            if (event.widget == buttonOK) {
                deleteFlag = true;
            } else {
                deleteFlag = false;
            }
            dialog.close();
        }
    };

    buttonOK.addListener(SWT.Selection, listener);
    buttonCancel.addListener(SWT.Selection, listener);

    Listener buttonListener = new Listener() {
        public void handleEvent(Event event) {
            dialog.open();
        }
    };

    button.addListener(SWT.Selection, buttonListener);

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

    if (deleteFlag) {
        text.setText("File deleted.");
    } else {
        text.setText("File not deleted.");
    }

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

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

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

    // (works with either a Label or a READ_ONLY Text)
    final Label liveLabel = new Label(shell, SWT.BORDER | SWT.READ_ONLY);
    //   final Text liveLabel = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
    liveLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    liveLabel.setText("Live region messages go here");

    new Label(shell, SWT.NONE).setText("Type an integer from 1 to 4:");

    final Text textField = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    textField.addModifyListener(e -> {
        String textValue = textField.getText();
        String message = textValue + " is not valid input.";
        try {//  www  .  j  av  a 2 s.c  om
            int value = Integer.parseInt(textValue);
            switch (value) {
            case 1:
                message = "One for the money,";
                break;
            case 2:
                message = "Two for the show,";
                break;
            case 3:
                message = "Three to get ready,";
                break;
            case 4:
                message = "And four to go!";
                break;
            }
        } catch (NumberFormatException ex) {
        }
        liveLabel.setText(message);
        textField.getAccessible().sendEvent(ACC.EVENT_DESCRIPTION_CHANGED, null);
        textField.setSelection(0, textField.getCharCount());
    });
    textField.getAccessible().addRelation(ACC.RELATION_DESCRIBED_BY, liveLabel.getAccessible());
    liveLabel.getAccessible().addRelation(ACC.RELATION_DESCRIPTION_FOR, textField.getAccessible());
    textField.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getDescription(AccessibleEvent event) {
            event.result = liveLabel.getText();
        }
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Color red = display.getSystemColor(SWT.COLOR_RED);
    final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Shell shell = new Shell(display);
    shell.setText("Snippet 68");
    shell.setLayout(new RowLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Stop Timer");
    final Label label = new Label(shell, SWT.BORDER);
    label.setBackground(red);//from   w w  w.j a va  2  s  .  c o m
    final int time = 500;
    final Runnable timer = new Runnable() {
        @Override
        public void run() {
            if (label.isDisposed())
                return;
            Color color = label.getBackground().equals(red) ? blue : red;
            label.setBackground(color);
            display.timerExec(time, this);
        }
    };
    display.timerExec(time, timer);
    button.addListener(SWT.Selection, event -> display.timerExec(-1, timer));
    button.pack();
    label.setLayoutData(new RowData(button.getSize()));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:SWTButtonAction.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(200, 200);//from w w  w.j a  va2 s. c  o m
    shell.setText("Dialogs");
    shell.open();

    final Button opener = new Button(shell, SWT.PUSH);
    opener.setText("Click Me");
    opener.setBounds(20, 20, 50, 25);

    final Text text = new Text(shell, SWT.SHADOW_IN);
    text.setBounds(80, 20, 100, 25);

    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
    dialog.setText("Dialog");
    dialog.setSize(150, 100);

    final Label label = new Label(dialog, SWT.NONE);
    label.setText("OK to proceed?");
    label.setBounds(35, 5, 100, 20);

    final Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setBounds(20, 35, 40, 25);
    okButton.setText("OK");

    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setBounds(70, 35, 40, 25);
    cancelButton.setText("Cancel");

    final boolean[] response = new boolean[1];
    response[0] = true;

    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            if (event.widget == okButton) {
                response[0] = true;
            } else {
                response[0] = false;
            }
            dialog.close();
        }
    };

    okButton.addListener(SWT.Selection, listener);
    cancelButton.addListener(SWT.Selection, listener);

    Listener openerListener = new Listener() {
        public void handleEvent(Event event) {
            dialog.open();
        }
    };

    opener.addListener(SWT.Selection, openerListener);

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

    if (response[0]) {
        text.setText("You clicked OK");
    } else {
        text.setText("You clicked Cancel");
    }

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

From source file:DragTextToLabel.java

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

    Text text = new Text(shell, SWT.BORDER | SWT.SINGLE);

    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    DragSource source = new DragSource(text, DND.DROP_MOVE | DND.DROP_COPY);
    source.setTransfer(types);//w  ww .  j  a  v a  2s  .c o  m

    source.addDragListener(new DragSourceAdapter() {
        public void dragSetData(DragSourceEvent event) {
            // Get the selected items in the drag source
            DragSource ds = (DragSource) event.widget;
            Text text = (Text) ds.getControl();

            event.data = text.getSelectionText();
        }
    });

    Label label = new Label(shell, SWT.BORDER);
    // Create the drop target
    DropTarget target = new DropTarget(label, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
        public void dragEnter(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT) {
                event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY : DND.DROP_NONE;
            }

            // Allow dropping text only
            for (int i = 0, n = event.dataTypes.length; i < n; i++) {
                if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i])) {
                    event.currentDataType = event.dataTypes[i];
                }
            }
        }

        public void dragOver(DropTargetEvent event) {
            event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
        }

        public void drop(DropTargetEvent event) {
            if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
                // Get the dropped data
                DropTarget target = (DropTarget) event.widget;
                Label label = (Label) target.getControl();
                String data = (String) event.data;

                label.setText(data);
                label.redraw();
            }
        }
    });

    text.setBounds(10, 10, 100, 25);
    label.setBounds(10, 55, 100, 25);
    shell.open();

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