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

public static Button createImageButton(Composite composite) {
    final Button button = new Button(composite, SWT.PUSH);
    Display display = composite.getDisplay();
    final Image image = new Image(display, "images/button1.gif");
    button.setImage(image);
    //       React to click events
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Key was pressed");
        }//from  w  w w .j a va2  s.  c  o  m
    });
    //       Dispose image when button is disposed
    button.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            image.dispose();
        }
    });
    return button;
}

From source file:ImageButton.java

public ImageButton() {
    //shell.setLayout(new RowLayout());

    Button button = new Button(shell, SWT.PUSH);
    button.setBounds(10, 10, 200, 200);/*from   ww  w  .j  a v a  2s  . c  om*/

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

    System.out.println(button.getImage());

    shell.pack();
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:SWTBrowser.java

public SWTBrowser() {
    shell.setLayout(new GridLayout());

    ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
    final ToolBarManager manager = new ToolBarManager(toolBar);

    Composite compositeLocation = new Composite(shell, SWT.NULL);
    compositeLocation.setLayout(new GridLayout(3, false));
    compositeLocation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Label labelAddress = new Label(compositeLocation, SWT.NULL);
    labelAddress.setText("Address");

    textLocation = new Text(compositeLocation, SWT.SINGLE | SWT.BORDER);
    textLocation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Button buttonGo = new Button(compositeLocation, SWT.NULL);
    buttonGo.setImage(new Image(shell.getDisplay(), "java2s.gif"));

    browser = new Browser(shell, SWT.BORDER);
    browser.setLayoutData(new GridData(GridData.FILL_BOTH));

    Composite compositeStatus = new Composite(shell, SWT.NULL);
    compositeStatus.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    compositeStatus.setLayout(new GridLayout(2, false));

    labelStatus = new Label(compositeStatus, SWT.NULL);
    labelStatus.setText("Ready");
    labelStatus.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    final ProgressBar progressBar = new ProgressBar(compositeStatus, SWT.SMOOTH);

    Listener openURLListener = new Listener() {
        public void handleEvent(Event event) {
            browser.setUrl(textLocation.getText());
        }//  w  w  w  .j  av  a  2  s  .c  o m
    };

    buttonGo.addListener(SWT.Selection, openURLListener);
    textLocation.addListener(SWT.DefaultSelection, openURLListener);

    // Adds tool bar items using actions.
    final Action actionBackward = new Action("&Backword", ImageDescriptor.createFromFile(null, "java2s.gif")) {
        public void run() {
            browser.back();
        }
    };
    actionBackward.setEnabled(false); // action is disabled at start up.

    final Action actionForward = new Action("&Forward",
            ImageDescriptor.createFromFile(null, "icons/web/forward.gif")) {
        public void run() {
            browser.forward();
        }
    };
    actionForward.setEnabled(false); // action is disabled at start up.

    Action actionStop = new Action("&Stop", ImageDescriptor.createFromFile(null, "icons/web/stop.gif")) {
        public void run() {
            browser.stop();
        }
    };

    Action actionRefresh = new Action("&Refresh",
            ImageDescriptor.createFromFile(null, "icons/web/refresh.gif")) {
        public void run() {
            browser.refresh();
        }
    };

    Action actionHome = new Action("&Home", ImageDescriptor.createFromFile(null, "icons/web/home.gif")) {
        public void run() {
            browser.setUrl("http://www.eclipse.org");
        }
    };

    manager.add(actionBackward);
    manager.add(actionForward);
    manager.add(actionStop);
    manager.add(actionRefresh);
    manager.add(actionHome);

    manager.update(true);
    toolBar.pack();

    browser.addLocationListener(new LocationListener() {
        public void changing(LocationEvent event) {
            // Displays the new location in the text field.
            textLocation.setText(event.location);
        }

        public void changed(LocationEvent event) {
            // Update tool bar items.
            actionBackward.setEnabled(browser.isBackEnabled());
            actionForward.setEnabled(browser.isForwardEnabled());
            manager.update(false);
        }
    });

    browser.addProgressListener(new ProgressListener() {
        public void changed(ProgressEvent event) {
            progressBar.setMaximum(event.total);
            progressBar.setSelection(event.current);
        }

        public void completed(ProgressEvent event) {
            progressBar.setSelection(0);
        }
    });

    browser.addStatusTextListener(new StatusTextListener() {
        public void changed(StatusTextEvent event) {
            labelStatus.setText(event.text);
        }
    });

    browser.addTitleListener(new TitleListener() {
        public void changed(TitleEvent event) {
            shell.setText(event.title + " - powered by SWT");
        }
    });

    initialize(display, browser);

    shell.setSize(500, 400);
    shell.open();
    //textUser.forceFocus();

    //browser.setText(
    //   "<html><body>" + "<h1>SWT &amp; JFace </h1>" + "</body/html>");

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}