0

I am trying to create a button with an image. The API can be found here.

Here is my code:

Button settings = new Button(swpContainer, SWT.PUSH);
settings.setText("Settings");
settings.setImage(new Image(null, "/myProject/icons/settings.png"));

And here is the exception I am getting..

org.eclipse.swt.SWTException: i/o error (java.io.FileNotFoundException:    \myProject\icons\settings.png (The system cannot find the path specified))

I am right clicking the image in eclipse and getting the path from the properties. Any help will be greatly appreciated!

Chris Bolton
  • 2,162
  • 4
  • 36
  • 75

3 Answers3

2

Try this:

Button settings = new Button(swpContainer, SWT.PUSH);
settings.setText("Settings");
Image image = new Image(swpContainer.getShell().getDisplay(), 
          getClass().getClassLoader().getResourceAsStream("icons/settings.png")); //$NON-NLS-1$
settings.setImage(image);
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
1

Presuming myProject is your project name, and icons is a simple folder in the project, then saying new Image(null, "icons/settings.png"); is enough.

I also recommend:

Community
  • 1
  • 1
Georgian
  • 8,795
  • 8
  • 46
  • 87
1

If you are writing it for RCP application,try using ImageDescriptor and create image.

Image IMG_EXAMPLE = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
      "/icons/settings.png").createImage();
Santhosh
  • 534
  • 9
  • 24