10.9. Creating SWT Browser Widgets

Problem

You want to give users some online access—for example, to access online help.

Solution

Use an SWT browser widget. Starting in Eclipse 3.0, SWT contains a browser widget built in.

Discussion

In Eclipse 2.x, you do not have much choice for displaying browsers in SWT windows. You really can do so only in Windows, using OLE. Here’s how it works. You create an OleFrame object and connect the system’s browser to it:

import org.eclipse.swt.ole.win32.*;
        .
        .
        .
        OleControlSite oleControlSite;
        
        OleFrame oleFrame = new OleFrame(shell, SWT.NONE);
        oleControlSite = new OleControlSite(oleFrame, SWT.NONE, "Shell.Explorer");
        oleControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
        shell.open( );
        .
        .
        .

Then you create an OleAutomation object and navigate to the URL you want like so:

import org.eclipse.swt.ole.win32.*;
        .
        .
        .
        OleControlSite oleControlSite;
        
        OleFrame oleFrame = new OleFrame(shell, SWT.NONE);
        oleControlSite = new OleControlSite(oleFrame, SWT.NONE, "Shell.Explorer");
        oleControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
        shell.open( );
        
        final OleAutomation browser = new OleAutomation(oleControlSite);
    
               int[] browserIDs = browser.getIDsOfNames(new String[]{"Navigate", "URL"}); 
               Variant[] address = new Variant[] {new Variant("http://www.oreilly.com")};
               browser.invoke(browserIDs[0], address, new int[]{browserIDs[1]});

Eclipse 3.0

In Eclipse 3.x, life is easier because SWT comes with a built-in browser widget. This widget currently is supported in Windows (using Internet ...

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.