Search code examples
eclipse-pluginswt

How to set background image to shell or composite in SWT


public class createShell{

        System.out.println("Inside shell create");
        display = new Display();
        shell = new Shell(display);
        shell.setSize(990, 590);
       
        shell.setLayout(new GridLayout());
        
        Composite comp = new Composite(shell, SWT.NO_FOCUS);
        comp.setBounds(10, 10, 720, 400);
        
}

I have existing code like this. Need to set a background image to shell. How to give relative path of image. The folder structure of plug-in(com.vcc.rac.ks5lmpp) is as per image.path The code file is in package inside src folder(plugins\com.rac.ks5lmpp\src\com\kjj\rac\ks5lmpp\stylesheets\dialogs\createShell.java) There exists a image folder(plugins\com.rac.ks5lmpp\src\com\kjj\rac\ks5lmpp\stylesheets\dialogs\images) in which the image is there which I want to pass as background. Image size is of 676X324 of PNG type.

Thanks in advance.

path


Solution

  • You use FileLocator in an Eclipse plug-in to find resources in the plug-in.

    Bundle bundle = FrameworkUtil.getBundle(getClass()); // Or some other way to find the current bundle
    
    URL url = FileLocator.find(bundle, new Path("path within the plugin"));
    
    ImageDescriptor desc = ImageDescriptor.createFromURL(url);
    
    Image image = desc.createImage();
    
    shell.setBackgroundImage(image);
    

    Note: You should arrange to dispose of the image when the shell closes.

    Be sure that the build.properties for the plug-in includes the images.

    FileLocator is org.eclipse.core.runtime.FileLocator.

    Path is org.eclipse.core.runtime.Path (not the java.nio.file Path)

    ImageDescriptor is org.eclipse.jface.resource.ImageDescriptor