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

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

Introduction

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

Prototype

public Image getImage() 

Source Link

Document

Returns the receiver's image if it has one, or null if it does not.

Usage

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 ww  w  .  ja  v a  2 s .co m
        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:ControlListenerExample.java

/**
 * Creates the main window's contents//from  w  w w .  j  av  a  2 s .c o m
 * 
 * @param shell the main window
 * @param image the image
 */
private void createContents(Shell shell, Image image) {
    shell.setLayout(new GridLayout());

    // Create a label to hold the image
    Label label = new Label(shell, SWT.NONE);
    label.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
    label.setImage(image);
    shell.setData(label);

    // Add the listener
    shell.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent event) {
            // Get the event source (the shell)
            Shell shell = (Shell) event.getSource();

            // Get the source's data (the label)
            Label label = (Label) shell.getData();

            // Determine how big the shell should be to fit the image
            Rectangle rect = shell.getClientArea();
            ImageData data = label.getImage().getImageData();

            // If the shell is too small, hide the image
            if (rect.width < data.width || rect.height < data.height) {
                shell.setText("Too small.");
                label.setText("I'm melting!");
            } else {
                // He fits!
                shell.setText("Happy Guy Fits!");
                label.setImage(label.getImage());
            }
        }
    });
}