SWT browser widgets and local plugin files

Ok, getting very technical from my normal postings but I found this really difficult to track down so thought it was worth posting somewhere. I’ve creating an application inside the Eclipse framework and wanted to write some HTML and display inside the SWT browser widget. It’s pretty easy to do this with files elsewhere, you simply give the browser widget a URL. However, if you want to load stuff from a local project it turns out to be a bit less obvious (technically this applies to getting access to any files from within an Eclipse project at runtime). So… in great anticipation I’m sure - here’s the code:

Browser browser = new Browser(parent, SWT.NONE);
Bundle plugin = Activator.getDefault().getBundle(); // Where Activator is my org.eclipse.core.runtime.Plugin
IPath relativePagePath = new Path("html/index.html");
URL fileInPlugin = FileLocator.find(plugin, relativePagePath, null);
URL pageUrl = FileLocator.toFileURL(fileInPlugin);
browser.setUrl(pageUrl.toString());

Easy when you know how.

Posted in techy solutions.

Trackbacks

blog comments powered by Disqus