Example usage for org.eclipse.swt.widgets Button setImage

List of usage examples for org.eclipse.swt.widgets Button setImage

Introduction

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

Prototype

public void setImage(Image image) 

Source Link

Document

Sets the receiver's image to the argument, which may be null indicating that no image should be displayed.

Usage

From source file:ButtonImageAdd.java

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

    shell.setLayout(new FillLayout());

    Image image = new Image(display, "yourFile.gif");

    Button button = new Button(shell, SWT.PUSH);

    button.setImage(image);
    button.setText("button");

    shell.open();//  w w w .  ja  v  a  2 s  . com
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

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

public static void main(String[] args) {
    Display display = new Display();
    Image image = display.getSystemImage(SWT.ICON_QUESTION);
    Shell shell = new Shell(display);
    shell.setText("Snippet 206");
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setImage(image);
    button.setText("Button");
    shell.setSize(300, 300);/* w w  w. j  a v a 2  s  . c  o m*/
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 164");
    shell.setLayout(new GridLayout());
    Image image = new Image(display, Snippet164.class.getResourceAsStream("eclipse.png"));

    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("&Typical button");

    Button button2 = new Button(shell, SWT.PUSH);
    button2.setImage(image);
    button2.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override/*from w ww  .j  a  v  a2  s.  com*/
        public void getName(AccessibleEvent e) {
            e.result = "Eclipse logo";
        }
    });

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

From source file:ImageRegistryUsing.java

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

    ImageRegistry ir = new ImageRegistry();
    ir.put("image1", new Image(display, "yourFile.gif"));

    Button executeButton = new Button(shell, SWT.PUSH);
    executeButton.setText("Execute");
    executeButton.setImage(ir.get("image1"));

    shell.open();//  w w w  .j  a  v  a2  s  .c o  m
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Color red = display.getSystemColor(SWT.COLOR_RED);
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    Color black = display.getSystemColor(SWT.COLOR_BLACK);

    Image image = new Image(display, 20, 20);
    GC gc = new GC(image);
    gc.setBackground(red);//  w  ww.  ja  v  a 2 s . c  o m
    gc.fillRectangle(5, 5, 10, 10);
    gc.dispose();
    ImageData imageData = image.getImageData();

    PaletteData palette = new PaletteData(new RGB(0, 0, 0), new RGB(0xFF, 0xFF, 0xFF));
    ImageData maskData = new ImageData(20, 20, 1, palette);
    Image mask = new Image(display, maskData);
    gc = new GC(mask);
    gc.setBackground(black);
    gc.fillRectangle(0, 0, 20, 20);
    gc.setBackground(white);
    gc.fillRectangle(5, 5, 10, 10);
    gc.dispose();
    maskData = mask.getImageData();

    Image icon = new Image(display, imageData, maskData);
    Shell shell = new Shell(display);
    shell.setText("Snippet 70");
    Button button = new Button(shell, SWT.PUSH);
    button.setImage(icon);
    button.setSize(60, 60);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    icon.dispose();
    image.dispose();
    mask.dispose();
    display.dispose();
}

From source file:IconInMemory.java

public static void main(String[] args) {
    Display display = new Display();
    Color red = display.getSystemColor(SWT.COLOR_RED);
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    Color black = display.getSystemColor(SWT.COLOR_BLACK);

    Image image = new Image(display, 20, 20);
    GC gc = new GC(image);
    gc.setBackground(red);/*from ww  w  .j  a v a 2  s .c o  m*/
    gc.fillRectangle(5, 5, 10, 10);
    gc.dispose();
    ImageData imageData = image.getImageData();

    PaletteData palette = new PaletteData(new RGB[] { new RGB(0, 0, 0), new RGB(0xFF, 0xFF, 0xFF), });
    ImageData maskData = new ImageData(20, 20, 1, palette);
    Image mask = new Image(display, maskData);
    gc = new GC(mask);
    gc.setBackground(black);
    gc.fillRectangle(0, 0, 20, 20);
    gc.setBackground(white);
    gc.fillRectangle(5, 5, 10, 10);
    gc.dispose();
    maskData = mask.getImageData();

    Image icon = new Image(display, imageData, maskData);
    Shell shell = new Shell(display);
    Button button = new Button(shell, SWT.PUSH);
    button.setImage(icon);
    button.setSize(60, 60);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    icon.dispose();
    image.dispose();
    mask.dispose();
    display.dispose();
}

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

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

    Image i = new Image(display, Snippet364.class.getResourceAsStream("eclipse.png"));
    Button b = new Button(shell, SWT.PUSH | SWT.LEFT_TO_RIGHT);
    b.setText("Button LEFT_TO_RIGHT...");
    b.setImage(i);

    Button b2 = new Button(shell, SWT.PUSH | SWT.RIGHT_TO_LEFT);
    b2.setText("Button RIGHT_TO_LEFT...");
    b2.setImage(i);/*  w w  w  .  ja v  a 2  s  .  com*/

    new Label(shell, SWT.NONE).setText("with FLIP_TEXT_DIRECTION:");

    Button b3 = new Button(shell, SWT.PUSH | SWT.LEFT_TO_RIGHT | SWT.FLIP_TEXT_DIRECTION);
    b3.setText("Button LEFT_TO_RIGHT...");
    b3.setImage(i);

    Button b4 = new Button(shell, SWT.PUSH | SWT.RIGHT_TO_LEFT | SWT.FLIP_TEXT_DIRECTION);
    b4.setText("Button RIGHT_TO_LEFT...");
    b4.setImage(i);

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

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

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

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button"); // the first button's accessible name is "Button"

    Image image = new Image(display, AccessibleNameExample.class.getResourceAsStream("run.gif"));
    button = new Button(shell, SWT.PUSH);
    button.setImage(image);
    button.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override//from   w  w w .  j a v a2  s.  c  o  m
        public void getName(AccessibleEvent e) {
            e.result = "Running man"; // the second button's accessible name is "Running man"
        }
    });

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

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

@SuppressWarnings("restriction")
public static void main(String[] args) {
    System.setProperty("swt.autoScale", "quarter");
    Display display = new Display();
    final Image eclipse = new Image(display, filenameProvider);
    final Image eclipseToolBar1 = new Image(display, filenameProvider);
    final Image eclipseToolBar2 = new Image(display, filenameProvider);
    final Image eclipseTableHeader = new Image(display, filenameProvider);
    final Image eclipseTableItem = new Image(display, filenameProvider);
    final Image eclipseTree1 = new Image(display, filenameProvider);
    final Image eclipseTree2 = new Image(display, filenameProvider);
    final Image eclipseCTab1 = new Image(display, filenameProvider);
    final Image eclipseCTab2 = new Image(display, filenameProvider);

    Shell shell = new Shell(display);
    shell.setText("Snippet 373");
    shell.setImage(eclipse);/*from   w  ww.ja  v a 2 s  .c  o  m*/
    shell.setText("DynamicDPI @ " + DPIUtil.getDeviceZoom());
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    shell.setLocation(100, 100);
    shell.setSize(500, 600);
    shell.addListener(SWT.ZoomChanged, new Listener() {
        @Override
        public void handleEvent(Event e) {
            if (display.getPrimaryMonitor().equals(shell.getMonitor())) {
                MessageBox box = new MessageBox(shell, SWT.PRIMARY_MODAL | SWT.OK | SWT.CANCEL);
                box.setText(shell.getText());
                box.setMessage("DPI changed, do you want to exit & restart ?");
                e.doit = (box.open() == SWT.OK);
                if (e.doit) {
                    shell.close();
                    System.out.println("Program exit.");
                }
            }
        }
    });

    // Menu
    Menu bar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(bar);
    MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
    fileItem.setText("&File");
    fileItem.setImage(eclipse);
    Menu submenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(submenu);
    MenuItem subItem = new MenuItem(submenu, SWT.PUSH);
    subItem.addListener(SWT.Selection, e -> System.out.println("Select All"));
    subItem.setText("Select &All\tCtrl+A");
    subItem.setAccelerator(SWT.MOD1 + 'A');
    subItem.setImage(eclipse);

    // CTabFolder
    CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 2; i++) {
        CTabItem cTabItem = new CTabItem(folder, i % 2 == 0 ? SWT.CLOSE : SWT.NONE);
        cTabItem.setText("Item " + i);
        Text textMsg = new Text(folder, SWT.MULTI);
        textMsg.setText("Content for Item " + i);
        cTabItem.setControl(textMsg);
        cTabItem.setImage((i % 2 == 1) ? eclipseCTab1 : eclipseCTab2);
    }

    // PerMonitorV2 setting
    //      Label label = new Label (shell, SWT.BORDER);
    //      label.setText("PerMonitorV2 value before:after:Error");
    //      Text text = new Text(shell, SWT.BORDER);
    //      text.setText(DPIUtil.BEFORE + ":" + DPIUtil.AFTER + ":" + DPIUtil.RESULT);

    // Composite for Label, Button, Tool-bar
    Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new RowLayout(SWT.HORIZONTAL));

    // Label with Image
    Label label1 = new Label(composite, SWT.BORDER);
    label1.setImage(eclipse);

    // Label with text only
    Label label2 = new Label(composite, SWT.BORDER);
    label2.setText("No Image");

    // Button with text + Old Image Constructor
    Button oldButton1 = new Button(composite, SWT.PUSH);
    oldButton1.setText("Old Img");
    oldButton1.setImage(new Image(display, IMAGE_PATH_100));

    // Button with Old Image Constructor
    //      Button oldButton2 = new Button(composite, SWT.PUSH);
    //      oldButton2.setImage(new Image(display, filenameProvider.getImagePath(100)));

    // Button with Image
    Button createDialog = new Button(composite, SWT.PUSH);
    createDialog.setText("Child Dialog");
    createDialog.setImage(eclipse);
    createDialog.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.RESIZE);
            dialog.setText("Child Dialog");
            RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
            dialog.setLayout(rowLayout);
            Label label = new Label(dialog, SWT.BORDER);
            label.setImage(eclipse);
            Point location = shell.getLocation();
            dialog.setLocation(location.x + 250, location.y + 50);
            dialog.pack();
            dialog.open();
        }
    });

    // Toolbar with Image
    ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.BORDER);
    Rectangle clientArea = shell.getClientArea();
    toolBar.setLocation(clientArea.x, clientArea.y);
    for (int i = 0; i < 2; i++) {
        int style = i % 2 == 1 ? SWT.DROP_DOWN : SWT.PUSH;
        ToolItem toolItem = new ToolItem(toolBar, style);
        toolItem.setImage((i % 2 == 0) ? eclipseToolBar1 : eclipseToolBar2);
        toolItem.setEnabled(i % 2 == 0);
    }
    toolBar.pack();

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Refresh-Current Monitor : Zoom");
    Text text1 = new Text(shell, SWT.BORDER);
    Monitor monitor = button.getMonitor();
    text1.setText("" + monitor.getZoom());
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent e) {
            Monitor monitor = button.getMonitor();
            text1.setText("" + monitor.getZoom());
        }
    });
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Refresh-Both Monitors : Zoom");
    Text text2 = new Text(shell, SWT.BORDER);
    Monitor[] monitors = display.getMonitors();
    StringBuilder text2String = new StringBuilder();
    for (int i = 0; i < monitors.length; i++) {
        text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : ""));
    }
    text2.setText(text2String.toString());
    button2.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent e) {
            Monitor[] monitors = display.getMonitors();
            StringBuilder text2String = new StringBuilder();
            for (int i = 0; i < monitors.length; i++) {
                text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : ""));
            }
            text2.setText(text2String.toString());
        }
    });

    // Table
    Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    String titles[] = { "Title 1" };
    for (int i = 0; i < titles.length; i++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(titles[i]);
        column.setImage(eclipseTableHeader);
    }
    for (int i = 0; i < 1; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(0, "Data " + i);
        item.setImage(0, eclipseTableItem);
    }
    for (int i = 0; i < titles.length; i++) {
        table.getColumn(i).pack();
    }

    // Tree
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 1; i++) {
        TreeItem iItem = new TreeItem(tree, 0);
        iItem.setText("TreeItem (0) -" + i);
        iItem.setImage(eclipseTree1);
        TreeItem jItem = null;
        for (int j = 0; j < 1; j++) {
            jItem = new TreeItem(iItem, 0);
            jItem.setText("TreeItem (1) -" + j);
            jItem.setImage(eclipseTree2);
            jItem.setExpanded(true);
        }
        tree.select(jItem);
    }

    // Shell Location
    Monitor primary = display.getPrimaryMonitor();
    Rectangle bounds = primary.getBounds();
    Rectangle rect = shell.getBounds();
    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;
    shell.setLocation(x, y);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Clipboard clipboard = new Clipboard(display);
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setText("Snippet 282");
    shell.setLayout(new GridLayout());
    shell.setText("Clipboard ImageTransfer");

    final Button imageButton = new Button(shell, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.minimumHeight = 400;/* w  w  w.  j  a v  a 2 s .  c om*/
    gd.minimumWidth = 600;
    imageButton.setLayoutData(gd);

    final Text imageText = new Text(shell, SWT.BORDER);
    imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    Composite buttons = new Composite(shell, SWT.NONE);
    buttons.setLayout(new GridLayout(4, true));
    Button button = new Button(buttons, SWT.PUSH);
    button.setText("Open");
    button.addListener(SWT.Selection, event -> {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setText("Open an image file or cancel");
        String string = dialog.open();
        if (string != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null)
                image.dispose();
            image = new Image(display, string);
            imageButton.setImage(image);
            imageText.setText(string);
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Copy");
    button.addListener(SWT.Selection, event -> {
        Image image = imageButton.getImage();
        if (image != null) {
            ImageTransfer imageTransfer = ImageTransfer.getInstance();
            TextTransfer textTransfer = TextTransfer.getInstance();
            clipboard.setContents(new Object[] { image.getImageData(), imageText.getText() },
                    new Transfer[] { imageTransfer, textTransfer });
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Paste");
    button.addListener(SWT.Selection, event -> {
        ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
        if (imageData != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null)
                image.dispose();
            image = new Image(display, imageData);
            imageButton.setImage(image);
        } else {
            imageButton.setText("No image");
            imageButton.setImage(null);
        }
        String text = (String) clipboard.getContents(TextTransfer.getInstance());
        if (text != null) {
            imageText.setText(text);
        } else {
            imageText.setText("");
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Clear");
    button.addListener(SWT.Selection, event -> {
        imageButton.setText("");
        Image image = imageButton.getImage();
        if (image != null)
            image.dispose();
        imageButton.setImage(null);
        imageText.setText("");
    });

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