import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; public class IconTest { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout()); final Button button = new Button(shell, SWT.PUSH); GridData gd = new GridData(); gd.grabExcessHorizontalSpace = true; gd.minimumWidth = 100; gd.minimumHeight = 20; button.setLayoutData(gd); final ToolBar toolbar1 = new ToolBar(shell, SWT.FLAT); final ToolBar toolbar2 = new ToolBar(shell, SWT.FLAT); button.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { FileDialog dialog = new FileDialog(shell); dialog.setFilterExtensions(new String[] { "*.ico", "*.gif", "*.*" }); String name = dialog.open(); if (name == null) return; Image oldImage = button.getImage(); try { button.setImage(new Image(display, name)); ToolItem i1 = new ToolItem(toolbar1, SWT.NONE); i1.setImage(new Image(display, name)); InputStream is = new FileInputStream(name); ToolItem i2 = new ToolItem(toolbar2, SWT.NONE); i2.setImage(new Image(display, is)); is.close(); shell.redraw(); shell.layout(); if (oldImage != null) oldImage.dispose(); } catch (Exception e2) { } } }); shell.setSize(300, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }